How to add a Decorator to a Class Method

Laszlo Nagy gandalf at shopzeus.com
Tue Nov 20 14:04:28 EST 2007


>> Can new_func reference self?  Would self just be one of the args?
>>
>> -Greg
>>     
>
> For methods, self is always "just one of the args".  When the
> decorator is applied to a method, self will be args[0] in new_func.
>   
If you want to use the name "self" instead of args[0] you can:


def pre(fn):
    def new_func(self,*args,**kwargs):
       print "hi",self
       return fn(self,*args,**kwargs)
    return new_func



Best,

   Laszlo




More information about the Python-list mailing list