Using the following code, when I create an instance and then append an item to a list on the instance attribute, the instance method doesn't notice the added item.
class Stuff:
def __init__(self, stuff = []):
self.stuff = stuff
self.number_of_stuff = self.__len_stuff()
def __len_stuff(self):
return len(self.stuff)
# create an instance
some_stuff = Stuff(["notebook", "pencil", "eraser"])
# show stuff in instance
print(some_stuff.stuff)
# output: ['notebook', 'pencil', 'eraser']
# check how much stuff is in instance
print(some_stuff.number_of_stuff)
# output: 3
# add more stuff to instance
some_stuff.stuff.append("fidget spinner")
print(some_stuff.stuff)
# output: ['notebook', 'pencil', 'eraser', 'fidget spinner']
# check how much stuff is in instance again
print(some_stuff.number_of_stuff)
# output: 3
I am so confused by this. What is the proper way to append an item to an instance attribute list? It is as if somehow the instance method "can't see" all of the items in the list.
number_of_stuffproperty when the object is first initialised - it is never changed afterwards.@propertyachieves.