我遇到Python在访问变量时抛出AttributeError的问题。
代码如下,为清晰起见进行了编辑。
class mycollection(object):
"""
Collection of stuff.
"""
#"compile-time" define class variables.
__slots__ = ["stuff_list"]
def __init__(self):
self.stuff_list = []
def add_stuff(self, stuff):
self.stuff_list.append(stuff)
#later on..
collection = mycollection()
stuff = stuff()
collection.add_stuff(stuff)
生成此错误。
Traceback (most recent call last):
File "", line 210, in <module>
main()
File "", line 206, in main
thestuff = load_file(inputfile, filetype)
File "pyyft.py", line 121, in load_file
collection.add_stuff(stuff)
File "pyyft.py", line 55, in add_test
self.stuff_list.append(stuff)
AttributeError: stuff_list
检查documentation,我不明白为什么会出现此错误。
答案 0 :(得分:2)
__ini__
应为__init__
答案 1 :(得分:0)
这不是“更多Pythonic”吗?
collection.stuff_list.append(test_stuff)