Stylistic question about inheritance

Lonnie Princehouse finite.automaton at gmail.com
Thu Mar 31 15:22:58 EST 2005


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

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): ...

or you could dynamically generate classes with inheritance based on a
grammar definition




More information about the Python-list mailing list