using super

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Dec 31 18:24:09 EST 2007


On Mon, 31 Dec 2007 08:03:22 -0800, Scott David Daniels wrote:

> Steven D'Aprano wrote:
>> ...
>> I'm not sure if this is your only problem or not, but super() only
>> works with new-style classes, not with classic classes. You must
>> inherit from object, or it cannot possibly work.
>> 
>> Change "class A" to "class A(object)".
> Absolutely correct.
> 
> However, the suggested simpler code cannot work on any released Python:
> 
>> def chain(meth):  # A decorator for calling super.
>>     def f(self, *args, **kwargs):
>>         result = meth(self, *args, **kwargs)
>>         S = super(self.__class__, self)
> This line is the problem.  The class parameter needs to be the class
> name (B in this case) in which the chaining method is defined, not that
> of the object itself.

One minor correction: the class parameter needs to be the class *itself*, 
not the class *name* (which would be the string "B").

I don't quite understand your description though. What do you mean "the 
chaining method is defined"? chain() is defined outside of a class.


[snip]

> You'll see the problem once you figure out what goes wrong with:
>    class C(B):
>        @chain
>        def foo(self, x):
>                print "This is C!!!"
>                return x + 2
> 
>    C().foo(5)


Hmmm... obviously I did insufficient testing. That's certainly a problem.



-- 
Steven



More information about the Python-list mailing list