开发者

Python AttributeError in using a member variable

开发者 https://www.devze.com 2023-01-06 11:02 出处:网络
I am having an issue with Python throwing an AttributeError on accessing a variable. The code is below, redacted for开发者_JAVA技巧 clarity.

I am having an issue with Python throwing an AttributeError on accessing a variable.

The code is below, redacted for开发者_JAVA技巧 clarity.

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)

Generating this error.

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

Checking through the documentation, I don't understand why this error is arising.


__ini__ should be __init__


Wouldn't this be "more Pythonic"?

collection.stuff_list.append(test_stuff)

0

精彩评论

暂无评论...
验证码 换一张
取 消