473,372 Members | 878 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 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 3386
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Humpty Dumpty | last post by:
Hello, I'm experimenting with different ways of extending a class (for a plug-ins framework for a GUI) with more than one extension when some of these extensions need to collaborate, but others...
4
by: Doug Holton | last post by:
First let me say please see the wiki page about python decorators if you haven't already: http://www.python.org/cgi-bin/moinmoin/PythonDecorators I propose (and others have) that built-in...
22
by: Ron_Adam | last post by:
Hi, Thanks again for all the helping me understand the details of decorators. I put together a class to create decorators that could make them a lot easier to use. It still has a few glitches...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
2
by: SpotNet | last post by:
Hi Newsgroup, Reconstructing my common dialog assembly using C# 2.0 .NET Framework 2.0. I have the following; public class FileDialogBase: System.Windows.Forms.FileDialog { //No constructor...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
7
by: MR | last post by:
Hello All, I have a question about decorators, and I think an illustration would be helpful. Consider the following simple class: #begin code class Foo: def fooDecorator(f): print...
20
by: vbgunz | last post by:
I remember learning closures in Python and thought it was the dumbest idea ever. Why use a closure when Python is fully object oriented? I didn't grasp the power/reason for them until I started...
0
by: Laszlo Nagy | last post by:
Aigars Aigars wrote: First of all, you should always inherit from "object" whenever it is possible. Then the answer: you did not return the result. Instead of self.func(*args, **kwargs)
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.