Potential improvement on delegation via explicit calls and super

Thomas Guettler guettli at thomas-guettler.de
Fri Dec 17 15:48:50 CET 2004


Am Fri, 17 Dec 2004 02:17:38 -0600 schrieb Robert Dick:

> Derived classes sometimes need to delegate portions of the work in overridden 
> methods to methods in their base classes.  This was traditionally done with 
> explicit calls in python, e.g.,
> 
> class Base:
>   def __init__(self):
>     print 'base'
> 
> class Left(Base):
>   def __init__(self, arg):
>     Base.__init__(self)
>     print 'left'
> 
> class Right(Base):
>   def __init__(self, arg):
>     Base.__init__(self)
>     print 'right'

If you can change the source of Base, I would do it like
this:

class Base:
    _init_done=0
    def __init__(self):
        if self._init_done:
            return
        self._init_done=1

        # ... do init


 Thomas

-- 
Thomas Güttler, http://www.thomas-guettler.de/





More information about the Python-list mailing list