代码之家  ›  专栏  ›  技术社区  ›  Paul Nathan

使用成员变量时出现python attributeError

  •  0
  • Paul Nathan  · 技术社区  · 14 年前

    我有一个关于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 回复  |  直到 14 年前
        1
  •  2
  •   Oddthinking    14 年前

    __ini__ 应该是 __init__

        2
  •  0
  •   storm_to    14 年前

    这不是“更多的蟒蛇”吗?

    collection.stuff_list.append(测试_-stuff)