tags:

views:

46

answers:

2

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

The code is below, redacted for 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.

+2  A: 

__ini__ should be __init__

Oddthinking
comment should be comment
SilentGhost
Durp. Accept incoming when the timer OK's me to.
Paul Nathan
I see that Jed Smith has edited the question, but I suspect that is the cause.
Oddthinking
could someone explain to me how `test_stuff` and `stuff_list` are related?
SilentGhost
@SilentGhost: manual redacting fail.
Paul Nathan
A: 

Wouldn't this be "more Pythonic"?

collection.stuff_list.append(test_stuff)

storm_to
It's not Pythonic to twiddle about with the internal state of objects, just because you can.
Chris B.
I don't grasp the entirety of the idea of 'pythonic', but I do know that what you propose presents a leakage of information between the object and the scope the object lives in. Put another way, it increases code coupling between modules. Put another way, it breaks OO paradigm. However you call it, it makes code maintenance and evolution more difficult.
Paul Nathan