method overriding trick
Gordon McMillan
gmcm at hypernet.com
Sat Nov 13 21:48:55 EST 1999
Eric Jacobs wrote:
> 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.
Unless you've nested your class statement inside a def or
another class, the class object will get built when the module
is loaded. So what the _Parent variable holds once your code
starts executing doesn't matter - it is only meaningful at the
time the "class" statement is executed.
Sure you can reassign it, but it will only mean something if
you do it at the top level.
OTOH, I find that it's rare for a base class and it's derived
classes to have the same __init__ signature, so for me the
trick is rarely useful.
- Gordon
More information about the Python-list
mailing list