Skip to content Skip to sidebar Skip to footer

Python Strange Behavior With List & Append

The following code is an issue I encountered and am looking for an explanation. The behavior of the code different than what I expected. Below the code will be my expected output,

Solution 1:

Compare this to your code

classUser():
    defsetup(self):
        self.listA = []                          # instance variable
        self.listB = []                          # instance variablefor i inrange(1):
            self.listA.append('a')
            self.listB.append('b')

Note that it's not necessary to "declare" any variables at the class level

Post a Comment for "Python Strange Behavior With List & Append"