views:

567

answers:

2

Am attempting to decorate a method inside a class but python is throwing an error on me. My class looks like this:

from pageutils import formatHeader

myPage(object):
   def __init__(self):
      self.PageName = ''

   def createPage(self):
      pageHeader = self.createHeader()

   @formatHeader   #<----- decorator
   def createHeader(self):
       return "Page Header ",self.PageName

if __name__=="__main__":
   page = myPage()
   page.PageName = 'My Page'
   page.createPage()

#------- pageutils.py --------------------

def formatHeader(fn):
   def wrapped():
       return '<div class="page_header">'+fn()+'</div>'
   return wrapped

Python throws the following error

self.createHeader() TypeError: wrapped() takes no arguments (1 given)

Where am i goofing

Gath

+4  A: 

Python automatically passes the class instance as reference. (The self argument which is seen in all class methods).

You could do:

def formatHeader(fn):
    def wrapped(self=None):
        return '<div class="page_header">'+fn(self)+'</div>'
    return wrapped
exhuma
oh... and in "createHeader", you are returning a tuple. Do this instead: `return "Page Header " + self.PageName`
exhuma
technically you could also do `def wrapped(self)`, unless if you want to use the decorator also outside of a class. But then the decorated functions need to deal with `self` gracefully!
exhuma
+4  A: 

You are omitting the self parameter which is present in the undecorated function (createHeader in your case).

def formatHeader(fn):
    from functools import wraps
    @wraps(fn)
    def wrapper(self):
        return '<div class="page_header">'+fn(self)+'</div>'
    return wrapper

If you are unsure about the signature of the function you want to decorate, you can make it rather general as follows:

def formatHeader(fn):
    from functools import wraps
    @wraps(fn)
    def wrapper(*args, **kw):
        return '<div class="page_header">'+fn(*args, **kw)+'</div>'
    return wrapper
krawyoti
+1 on `def wrapper(*args, **kw):`
Christian Oudard