method overriding trick

Eric Jacobs x at x.x
Sun Nov 14 01:16:25 CET 1999

Phil Mayes wrote:
> 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.

You wouldn't be able to reassign the _Parent variable to define another
class elsewhere in the same module, though, because the functions defined
this way will always look up the _Parent from the module namespace at
execution time. A variant that removes that restriction is:

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

The default args are evaluated at function definition time, which means
that the variable has the value you expect. Now you have true cut'n'paste

More information about the Python-list mailing list