method overriding trick

Jeremy Hylton jeremy at
Thu Nov 11 17:51:46 CET 1999

Overriding methods of a base class is a little clunky in Python.  You
need to explicitly name the base class that implements the method, and
you call its as an unbound method, passing self as the first argument.
Example: Base.aMethod(self, arg)

I was reading about MzScheme the other day (a Scheme variant with
object and interface support), and noticed that the MzScheme approach
translates well to Python.  MzScheme class defintions allow a rename
declarations that create a name in the class being defined that is
bound to a name in a superclass.  In Python, rename is just called

class Base:
    def aMethod(self, arg):
        return arg * 2

class Derived1(Base):
    super_aMethod = Base.aMethod

    def aMethod(self, arg):
        return self.super_aMethod(arg) + 1

I haven't seen this strategy for overriding mentioned before, although
I did not search the list archives.  When I described it to Guido, he
said, "Does that work?"  Has anyone used this trick before?

It doesn't solve the must-name-the-base-class problem, but it helps
manage it better.  If you keep the override assignments at the top of
the class statement, then you only have one place to look in each
class when the inheritance hierarchy changes.


More information about the Python-list mailing list