using super

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Dec 31 21:35:56 EST 2007


On Mon, 31 Dec 2007 16:19:11 -0800, Scott David Daniels wrote:

> Steven D'Aprano wrote:
>> On Mon, 31 Dec 2007 08:03:22 -0800, Scott David Daniels wrote:
>>> Steven D'Aprano wrote: ...
>>>> 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
>>> (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").
> Point taken.
> 
>> I don't quite understand your description though. What do you mean "the
>> chaining method is defined"? chain() is defined outside of a class.
>
> The class where f (the chaining method) is defined; equivalently, the
> class in which the @chain is used.

So why doesn't self.__class__ work? That's the class in which @chain is 
used.

I can clearly see that it doesn't work, I just don't understand why. I'd 
be inclined to chalk it up to super() being a mysterious black box that 
makes no sense *wink* except that the following decorator also doesn't 
work:


def chain(meth):  # A decorator for not calling super.
    def f(self, *args, **kwargs):
        result = meth(self, *args, **kwargs)
        S = self.__class__.__base__
        getattr(S, meth.__name__)(self, *args, **kwargs)
        return result
    return f



-- 
Steven



More information about the Python-list mailing list