Stylistic question about inheritance

Andrew Koenig ark at acm.org
Thu Mar 31 15:28:17 EST 2005


"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.

> I usually skip the inheritance and make everything an instance of the
> same class, e.g.
>
> class ASTNode(object): ...
>
> class Stmt(ASTNode): ...
> class Expr(ASTNode): ...
> class UnaryExpr(ASTNode): ...
> class BinaryExpr(ASTNode): ...

Eh?  There's still inheritance here: Everything is derived from ASTNode.  I 
understand that there is a separate design issue whether to make the 
hierarchy deep or shallow, but it's still a hierarchy.






More information about the Python-list mailing list