python 对象初始化期间属性错误
蟒蛇2.6.7; Windows 7
用 Python 解决 Deitel“简单编译器”练习。存储库位于 github。编写测试套件。
module compiler.py
class SCompiler( object ) :
RAMSIZE = 100
# more static attributes
def __init__( self ) :
self.symbolTable = [ TableEntry( ) ] * SCompiler.RAMSIZE
self.lineFlags[ -1 ] * SCompiler.RAMSIZE
# more initializations
堆栈跟踪。
File "testCompiler.py", line 45, in <module>
tool = compiler.SCompiler( )
File "absolutePath\compiler.py", line 37, in `__init__`
self.lineFlags[ -1 ] * SCompiler.RAMSIZE
AttributeError: 'SCompiler' object has no attribute 'lineFlags'
我不确定这里有什么拼写错误。文件“compiler.py”文件进行静默编译。 TableEntry 是文件中的另一个类。谢谢你的建议。
Python 2.6.7; Windows 7
Solving Deitel 'Simple Compiler' exercise in python. Repository at github. Writing a test suite.
module compiler.py
class SCompiler( object ) :
RAMSIZE = 100
# more static attributes
def __init__( self ) :
self.symbolTable = [ TableEntry( ) ] * SCompiler.RAMSIZE
self.lineFlags[ -1 ] * SCompiler.RAMSIZE
# more initializations
Stack trace.
File "testCompiler.py", line 45, in <module>
tool = compiler.SCompiler( )
File "absolutePath\compiler.py", line 37, in `__init__`
self.lineFlags[ -1 ] * SCompiler.RAMSIZE
AttributeError: 'SCompiler' object has no attribute 'lineFlags'
I'm not sure what there is to misspell here. File 'compiler.py' file compiles silently. TableEntry is another class in the file. Thanks for your advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您会得到
AttributeError
,因为它在知道
lineFlag
是什么之前调用:。但从下面的行来看(来自文件 compiler.py 在您链接的存储库中):
我认为作者忘记了
=
,请尝试将 : 替换为:
You get
AttributeError
, because it calls:before knowing what
lineFlag
is.But judging by the line below (from the file compiler.py in the repository that you linked):
I think that the author forgot an
=
, try to replace :with:
在尝试将其视为数组之前,您必须初始化 self.lineFlags 。
You have to initialize self.lineFlags before trying to treat it like an array.