HOWTO restrict multiply inherited class to on sub-class

Michael Hudson mwh21 at cam.ac.uk
Mon Apr 2 11:31:44 EDT 2001


Michael Hudson <mwh21 at cam.ac.uk> writes:

> > any clues, anyone?
> 
> Try this:

Or, on second thoughts, don't.  It will call Base1's methods even if
they're overridden in Derived, something you almost certainly don't
want.  You could change Casted.__getattr__ to 

    def __getattr__(self, attr):
        try:
            return self.wrapped.__dict__[attr]
        except KeyError:
            try:
                getattr(self.klass, attr)
            except AttributeError:
                raise
            else:
                return getattr(self.wrapped, attr)

instead.

Cheers,
M.

-- 
3. Syntactic sugar causes cancer of the semicolon.
  -- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html



More information about the Python-list mailing list