Access self from decorator Access self from decorator python python

Access self from decorator


Since you're decorating a method, and self is a method argument, your decorator has access to self at runtime. Obviously not at parsetime, because there are no objects yet, just a class.

So you change your decorator to:

def decorator(func):    def _decorator(self, *args, **kwargs):        # access a from TestSample        print 'self is %s' % self        return func(self, *args, **kwargs)    return _decorator