Class decorators do not inherit properly
Bruno Desthuilliers
bruno.42.desthuilliers at wtf.websiteburo.oops.com
Fri Jul 13 03:37:31 EDT 2007
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
More information about the Python-list
mailing list