Basic inheritance question
MartinRinehart at gmail.com
MartinRinehart at gmail.com
Sat Jan 5 05:31:15 EST 2008
Working on parser for my language, I see that all classes (Token,
Production, Statement, ...) have one thing in common. They all
maintain start and stop positions in the source text. So it seems
logical to have them all inherit from a base class that defines those,
but this doesn't work:
import tok
class code:
def __init__( self, start, stop ):
startLoc = start
stopLoc = stop
class token(code):
pass
x = token( tok.Loc(0, 0), tok.Loc(3, 4) )
print x.startLoc.repr(), x.stopLoc.repr()
AttributeError: token instance has no attribute 'startLoc'
1) Is my design thinking good, or hopelessly unPythonic?
2) If it's good, how do you access base class data attributes? (The
doc is rich in method access info, impoverished when it comes to other
attributes.)
More information about the Python-list
mailing list