method overriding trick

Phil Mayes nospam at
Sat Nov 13 09:58:32 CET 1999

Jeremy Hylton wrote in message
<14378.62498.649973.150075 at>...
>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)

and suggested using

>class Derived1(Base):
>    super_aMethod = Base.aMethod
>    def aMethod(self, arg):
>        return self.super_aMethod(arg) + 1
>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.

I use a method that doesn't remove the clunkiness, but localises the
hierarchy to one line:

_Parent = basedlg.BaseDlg
class AddressDlg(_Parent):
    def __init__(self, param):
        self.local = param
        _Parent.__init__(self, 2345)

By using the same name "_Parent" in all modules, less thinking is needed,
and cut'n'paste coding (what, me?) works better too.
Phil Mayes    pmayes AT olivebr DOT com
Olive Branch Software - home of Arranger

More information about the Python-list mailing list