By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,271 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,271 IT Pros & Developers. It's quick & easy.

How to add a Decorator to a Class Method

P: n/a
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

Should result in:
>>a = A()
a.func(3,5)
'hi'
8

Thanks,

-Greg
Nov 20 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Nov 20, 4:59 am, "gregpin...@gmail.com" <gregpin...@gmail.com>
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

Nov 20 '07 #2

P: n/a
On Mon, 19 Nov 2007 20:59:51 -0800, gr********@gmail.com 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
At this point there is no `self` which is exactly what the exception says
if you run this. This method definition executed at class definition time
so there is no instance of `A`. You can't change it to ``@A.pre`` either
because the class is not fully constructed yet so the class name `A` does
not exist yet. So you have to move `pre()` out of the class.

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

class A(object):
@pre
def func(self, a, b):
print a + b

a = A()
a.func(3, 5)

Ciao,
Marc 'BlackJack' Rintsch
Nov 20 '07 #3

P: n/a
On Nov 20, 2:05 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Mon, 19 Nov 2007 20:59:51 -0800, gregpin...@gmail.com 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

At this point there is no `self` which is exactly what the exception says
if you run this. This method definition executed at class definition time
so there is no instance of `A`. You can't change it to `...@A.pre`` either
because the class is not fully constructed yet so the class name `A` does
not exist yet. So you have to move `pre()` out of the class.

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

class A(object):
@pre
def func(self, a, b):
print a + b

a = A()
a.func(3, 5)

Ciao,
Marc 'BlackJack' Rintsch

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 the args?

-Greg
Nov 20 '07 #4

P: n/a
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 the args?
Hi,

new_func is a dynamically created function. When pre() is called, it
will return this new function. (This happens when Python tries to excute
the "class A(object)" statement, in other words: it happens during class
definition.) The returned function is unound, and it assigned to A.func.
In other words, the function object is returned by pre(), but is not
called. "new_func" will only be called when you call a.func in your
example. When that happens, Python will find that that object 'a' has no
direct attribute called "func", but there is one in its class "A". So it
will call A.func as a bound method, with its "self" parameter set to the
object that was used to call the method -- namely, "a".

Hope this helps.

Laszlo

Nov 20 '07 #5

P: n/a
On Nov 20, 12:32 pm, "gregpin...@gmail.com" <gregpin...@gmail.com>
wrote:
On Nov 20, 2:05 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Mon, 19 Nov 2007 20:59:51 -0800, gregpin...@gmail.com 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
At this point there is no `self` which is exactly what the exception says
if you run this. This method definition executed at class definition time
so there is no instance of `A`. You can't change it to `...@A.pre`` either
because the class is not fully constructed yet so the class name `A` does
not exist yet. So you have to move `pre()` out of the class.
def pre(fn):
def new_func(*args, **kwargs):
print "'hi'"
fn(*args, **kwargs)
return new_func
class A(object):
@pre
def func(self, a, b):
print a + b
a = A()
a.func(3, 5)
Ciao,
Marc 'BlackJack' Rintsch

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 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.

--Nathan Davis
Nov 20 '07 #6

P: n/a
>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

Nov 20 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.