Stylistic question about inheritance
Guy Bolton King
Guy_Bolton-King at non.agilent.com
Fri Apr 1 07:49:44 EST 2005
"Andrew Koenig" <ark at acm.org> writes:
> "Lonnie Princehouse" <finite.automaton at gmail.com> wrote in message
> news:1112300578.456411.274110 at f14g2000cwb.googlegroups.com...
>
> > If you try this sort of inheritance, I'd recommend writing down the
> > formal grammar before you start writing classes. Don't try to define
> > the grammar through the inheritance hierarchy; it's too easy to
> > accidentally build a hierarchy that can't be translated into a
> > single-pass-parsable grammar...
>
> Understood. I was using expression trees as a contrived example, and really
> want to know about the Python community's stylistic preferences for defing
> such hierarchies that don't absolutely need a root.
Oddly enough, I've just been pondering the same question (albeit for
Perl, but the same reasoning applies). The only cases I've found
useful thus far are:
- implementation inheritance (in the case of default methods in a
callback interface class):
class CallbackInterface:
def handleEvent(self, event):
"""Handle an event"""
pass
def handleSignal(self, signal):
"""Handle a signal"""
pass
This also helps to document what's expected of callback classes,
even though they don't _have_ to inherit CallbackInterface
(enforcing this through isinstance() in the calling class would be
rude).
- hierarchies of exception classes (allowing one to catch general
classes of exceptions, since except implicitly uses isinstance(),
rather than a specific class). Of course, Python already has a
hierarchy of exceptions. I had to implement my own for Perl.
>From a brief skim of http://www.python.org/moin/PythonThreeDotOh it
looks like interfaces _may_ be added to Python 3.0, but they sound
more like (IIRC) ML's signatures and C++'s Standard Library
requirements i.e. a requirement that the class implements certain
functions, rather than a requirement to inherit from a particular base
class.
Guy.
More information about the Python-list
mailing list