Class decorators do not inherit properly

Chris Fonnesbeck listservs at mac.com
Thu Jul 12 20:49:11 CEST 2007


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.







More information about the Python-list mailing list