Q:

Python decorators in classes Error

class A(object):
    def wrapped(self, func):
        func.string = self.get_something()
        return func

    def get_something(self):
        return "something"

    @wrapped
    def main(self):
        print self.main.string
a = A()
a.main()

TypeError: wrapped() takes exactly 2 arguments (1 given)

A:

wrapped() is called at the definition of main().  When it is called, it is not given self, because there is no instance of A that can be given to wrapped().  To do what you want, don't use decorators.  Instead, define an __init__() method that assigns the right attributes.  I don't know why you want it to be an attribute of self.main(), however.  Generally it is not a good idea to add attributes to functions.  You already have a class; use it.  Just define self.string instead of self.main.string.

