我遇到了Python在訪問變量時拋出AttributeError的問題。使用成員變量的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檢查,我不明白爲什麼這個錯誤出現。
某處在你的代碼,你必須(ERR,可能)有字符串 「test_stuff」。請發佈該代碼。 – unutbu 2010-07-09 17:45:41
修復了'__init__'中的拼寫錯誤 - 如果您複製/粘貼了該文件,您可能需要在源代碼中修復它。另外,你使用'__slots__'的任何特定原因? – 2010-07-09 17:47:02
@Jed - 是的,我正在避免自動版本 – 2010-07-09 17:49:41