471,357 Members | 1,145 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 software developers and data experts.

Class decorators do not inherit properly

I have a class that does MCMC sampling (Python 2.5) that uses decorators
-- one in particular called _add_to_post that appends the output of the
decorated method to a class attribute. However, when I
subclass this base class, the decorator no longer works:

Traceback (most recent call last):
File "/Users/chris/Projects/CMR/closed.py", line 132, in <module>
class M0(MetropolisHastings):
File "/Users/chris/Projects/CMR/closed.py", line 173, in M0
@_add_to_post
NameError: name '_add_to_post' is not defined

yet, when I look at the dict of the subclass (here called M0), I see the
decorator method:

In [5]: dir(M0)
Out[5]:
['__call__',
'__doc__',
'__init__',
'__module__',
'_add_to_post',
....

I dont see what the problem is here -- perhaps someone could shed
some light. I thought it might be the underscore preceding the name,
but I tried getting rid of it and that did not help.

Thanks.


Jul 12 '07 #1
6 3294
Traceback (most recent call last):
File "/Users/chris/Projects/CMR/closed.py", line 132, in <module>
class M0(MetropolisHastings):
File "/Users/chris/Projects/CMR/closed.py", line 173, in M0
@_add_to_post
NameError: name '_add_to_post' is not defined

yet, when I look at the dict of the subclass (here called M0), I see the
decorator method:

I think the term "class decorator" is going to eventually
mean something other than what you are doing here. I'd
avoid the term for now.
When you decorate a class method, the function you use
needs to be defined before the method definition.

Using a class method to decorate another class method is
going to be tricky. The way I usually do it is to create
a separate function outside of the class definition for
the decorator function.
You're going to have to show us the actual code you are
having trouble with, or else (probably more useful, really)
try to put together a minimal example of what you are
trying to do and show us that code.

Jul 12 '07 #2
Lee Harr a écrit :
>Traceback (most recent call last):
File "/Users/chris/Projects/CMR/closed.py", line 132, in <module>
class M0(MetropolisHastings):
File "/Users/chris/Projects/CMR/closed.py", line 173, in M0
@_add_to_post
NameError: name '_add_to_post' is not defined

yet, when I look at the dict of the subclass (here called M0), I see the
decorator method:


I think the term "class decorator" is going to eventually
mean something other than what you are doing here. I'd
avoid the term for now.
When you decorate a class method,
the function you use
needs to be defined before the method definition.
This is true whatever you are decorating - class method, static method,
instance method, function. And FWIW, the term "class method" has a
definite meaning in Python.
Using a class method to decorate another class method is
going to be tricky. The way I usually do it is to create
a separate function outside of the class definition for
the decorator function.
The problem is then that this function cannot easily access the class
object - which is what the OP want.
>
You're going to have to show us the actual code you are
having trouble with, or else (probably more useful, really)
try to put together a minimal example of what you are
trying to do and show us that code.
+1 on this

Jul 13 '07 #3
Chris Fonnesbeck a écrit :
I have a class that does MCMC sampling (Python 2.5) that uses decorators
-- one in particular called _add_to_post that appends the output of the
decorated method to a class attribute.
However, when I
subclass this base class, the decorator no longer works:

Traceback (most recent call last):
File "/Users/chris/Projects/CMR/closed.py", line 132, in <module>
class M0(MetropolisHastings):
File "/Users/chris/Projects/CMR/closed.py", line 173, in M0
@_add_to_post
NameError: name '_add_to_post' is not defined

yet, when I look at the dict of the subclass (here called M0), I see the
decorator method:

In [5]: dir(M0)
Out[5]:
['__call__',
'__doc__',
'__init__',
'__module__',
'_add_to_post',
...

I dont see what the problem is here -- perhaps someone could shed
some light. I thought it might be the underscore preceding the name,
but I tried getting rid of it and that did not help.
A minimal runnable code snippet reproducing the problem would *really*
help, you know...

Anyway: the body of a class statement is it's own namespace. So in the
body of your base class, once the _add_to_post function is defined, you
can use it. But when subclassing, the subclass's class statement creates
a new namespace, in which _add_to_post is not defined - hence the
NameError. To access this symbol, you need to use a qualified name, ie:

class SubClass(BaseClass):
@BaseClass._add_to_post
def some_method(self):
# code here

Now there may be better solutions, but it's hard to tell without knowing
more about your concrete use case.

HTH
Jul 13 '07 #4
Chris Fonnesbeck schrieb:
I have a class that does MCMC sampling (Python 2.5) that uses decorators
-- one in particular called _add_to_post that appends the output of the
decorated method to a class attribute. However, when I
subclass this base class, the decorator no longer works:

Traceback (most recent call last):
File "/Users/chris/Projects/CMR/closed.py", line 132, in <module>
class M0(MetropolisHastings):
File "/Users/chris/Projects/CMR/closed.py", line 173, in M0
@_add_to_post
NameError: name '_add_to_post' is not defined

yet, when I look at the dict of the subclass (here called M0), I see the
decorator method:

In [5]: dir(M0)
Out[5]:
['__call__',
'__doc__',
'__init__',
'__module__',
'_add_to_post',
...

I dont see what the problem is here -- perhaps someone could shed
some light. I thought it might be the underscore preceding the name,
but I tried getting rid of it and that did not help.
Does this simple example show your problem?
class Meta(type):
def __init__(cls, *args):
print "Meta.__init__ called"
return super(Meta, cls).__init__(*args)
class A(object):
__metaclass__ = Meta

def decorator(f):
print "decorator called"
return f

@decorator
def foo(self):
pass
class B(A):
#@decorator
def bar(self):
pass
print dir(A())
print dir(B())
then it explains the problem easily: the class-statement (class
Name(base): <definitions) is evaluated _before_ the actual class is
created. Thus at that moment, there is no decorator known in the
surrounding scope.

Use a function level decorator instead, that delegates it's work to a
classmethod/instancemethod. Something like this (can't be more precise
as you didn't show us code):

def decorator(f):
def _d(self, *args, **kwargs):
self._a_decorator_method()
return f(self, *args, **kwargs)
return _d
Diez
Jul 13 '07 #5
>I think the term "class decorator" is going to eventually
>mean something other than what you are doing here. I'd
avoid the term for now.
When you decorate a class method,
the function you use
needs to be defined before the method definition.

FWIW, the term "class method" has a
definite meaning in Python.

Certainly. But "class decorator" is being introduced in
Python 3000 with PEP 3129:
http://www.python.org/dev/peps/pep-3129/

Jul 13 '07 #6
Lee Harr a écrit :
>>>I think the term "class decorator" is going to eventually
mean something other than what you are doing here. I'd
avoid the term for now.
When you decorate a class method,
the function you use
needs to be defined before the method definition.

FWIW, the term "class method" has a
definite meaning in Python.

Certainly. But "class decorator" is being introduced in
Python 3000 with PEP 3129:
http://www.python.org/dev/peps/pep-3129/
Certainly. But [1] "class methods" have been introduced in Python 2.2 !-)

[1] IIRC - please someone correct me if I'm wrong
Jul 16 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Humpty Dumpty | last post: by
22 posts views Thread by Ron_Adam | last post: by
reply views Thread by emin.shopper | last post: by
20 posts views Thread by vbgunz | last post: by
reply views Thread by Laszlo Nagy | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.