Think about the context in which your function definition is executed and how the 'self' parameter is implemented.   Your definitions (and decorator calls) are executed in the context of 'class A.'    <br><br>
That said, the following would work:<br><br>>>> class A:<br>    def pre(fn):<br>        def new_func(*args,**kwargs):<br>            print 'hi'<br>            fn(*args,**kwargs)<br>        return new_func
<br>    @pre<br>    def func(self,a,b):<br>        print a+b<br><br>>>> A().func(1,2)<br>hi<br>3<br>>>> <br><br><br><div><span class="gmail_quote">On 11/19/07, <b class="gmail_sendername"><a href="mailto:gregpinero@gmail.com">
gregpinero@gmail.com</a></b> <<a href="mailto:gregpinero@gmail.com">gregpinero@gmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
How do I add a decorator to a class method?  Here's what I want to do,<br>but I guess my syntax isn't right.  Any advice?<br><br>class A:<br>    def pre(self,fn):<br>        def new_func(*args,**kwargs):<br>            print 'hi'
<br>            fn(*args,**kwargs)<br>        return new_func<br>    @self.pre<br>    def func(self,a,b):<br>        print a+b<br><br>Should result in:<br>>>> a = A()<br>>>> a.func(3,5)<br>'hi'<br>
8<br><br>Thanks,<br><br>-Greg<br>--<br><a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br></blockquote></div><br>