On Nov 20, 4:59 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> How do I add a decorator to a class method?  Here's what I want to do,
> but I guess my syntax isn't right.  Any advice?
>
> class A:
>     def pre(self,fn):
>         def new_func(*args,**kwargs):
>             print 'hi'
>             fn(*args,**kwargs)
>         return new_func
>     @self.pre
>     def func(self,a,b):
>         print a+b

'self' is not bound during class creation so self.pre does not exist.
Your decorator should be a regular function:

def pre(fn):
    ...

class A:
    @pre
    def func(self, x, y):
        ....

HTH

--
Arnaud

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to