使用成员变量时的Python AttributeError

时间:2010-07-09 17:42:37

标签: python

我遇到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,我不明白为什么会出现此错误。

2 个答案:

答案 0 :(得分:2)

__ini__应为__init__

答案 1 :(得分:0)

这不是“更多Pythonic”吗?

collection.stuff_list.append(test_stuff)