Reuse base-class implementation of classmethod?

Giovanni Bajo noway at sorry.com
Tue Nov 1 10:18:25 CET 2005


David Wahler wrote:

>> what's the magic needed to reuse the base-class implementation of a
>> classmethod?
>>
>> class A(object):
>>    @classmethod
>>    def foo(cls, a,b):
>>        # do something
>>        pass
>>
>> class B(A):
>>     @classmethod
>>     def foo(cls, a, b):
>>          A.foo(cls, a, b)   # WRONG!
>>
>> I need to call the base-class classmethod to reuse its
>> implementation, but I'd like to pass the derived class as first
>> argument. --
>> Giovanni Bajo
>
> See the super object. In your case, it can be used like this:
>
> class B(A):
>     @classmethod
>     def foo(cls, a, b):
>         super(B, cls).foo(a, b)


Ah thanks, I did not realize that super() was the only way of doing the right
thing here!
-- 
Giovanni Bajo





More information about the Python-list mailing list