>> 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
On Nov 20, 12:32 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> On Nov 20, 2:05 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Mon, 19 Nov 2007 20:59:51 -0800, [EMAIL PROTECTED] wrote:
> > > How do I add a decorator to a class method? Here's what I want to do,
> > > but
> Thanks those answers make sense. But for this function if defined
> outside the class:
>
>
>> def pre(fn):
>> def new_func(*args, **kwargs):
>> print "'hi'"
>> fn(*args, **kwargs)
>> return new_func
>>
>
> Can new_func reference self? Would self just be one of t
On Nov 20, 2:05 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Mon, 19 Nov 2007 20:59:51 -0800, [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,f
On Mon, 19 Nov 2007 20:59:51 -0800, [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'
>
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'
>
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.'
That said, the following would work:
>>> class A:
def pre(fn):
def new_func(*args,**kwa
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,