Reuse base-class implementation of classmethod?

David Wahler dwahler at gmail.com
Mon Oct 31 21:52:15 EST 2005


Giovanni Bajo wrote:
> Hello,
>
> 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)

This all assumes you want to modify the behavior of foo in a subclass.
If not, of course, you don't need to override it at all.

-- David




More information about the Python-list mailing list