3

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, and the actual output. One last thing to note, is that I understand this code may be 'strange', and that using range(1) is a bit odd to say the least. The reason for this is that this exact occurrence in a program (the ranges were variables but at these values) caused problems.. so I made this simple code to replicate it.

userList = []

class User():
    listA = []
    listB = []

    def setup(self):
        for i in range(1):
            self.listA.append('a')
            self.listB.append('b')

for i in range(5):
    user = User()
    userList.append(user)

for i in range(len(userList)):
    userList[i].setup()

for i in range(len(userList)):
    print str(userList[i].listA)
    print str(userList[i].listB)

Expected Output

['a']
['b']
['a']
['b']
['a']
['b']
['a']
['b']
['a']
['b']

Actual Output

['a','a','a','a','a']
['b','b','b','b','b']
['a','a','a','a','a']
['b','b','b','b','b']
['a','a','a','a','a']
['b','b','b','b','b']
['a','a','a','a','a']
['b','b','b','b','b']
['a','a','a','a','a']
['b','b','b','b','b']

Discussion

I appreciate any explanation as to why this is happening. I'm not sure if the built-in append() function is somehow affecting all Users, or if each User is somehow sharing their fields. Running on Python 2.7.3.

user650658
  • 43
  • 2
  • 2
    `listA` is a class not an instance variable – Voo Jun 26 '12 at 22:27
  • Despite being inside the indentation for class User()? – user650658 Jun 26 '12 at 22:28
  • 1
    Yes it's a class variable despite being indented to the level of a class variable – John La Rooy Jun 26 '12 at 22:31
  • 1
    That's why it's a *class* not global variable, yes. It's probably a good idea to start at the python tutorial to get to know those small "quirks" in python that are different than in say Java. Still: To get a instance variable assign it to self, i.e.: `self.listA = []`. Edit: And who is downvoting again? This is an error that happens to *many* new python people and is generally extremely hard to google for.. – Voo Jun 26 '12 at 22:32
  • That makes sense. Thanks for the help, and sorry for the ignorance about Python classes! @Gnibbler & Voo – user650658 Jun 26 '12 at 22:34

1 Answers1

8

Compare this to your code

class User():
    def setup(self):
        self.listA = []                          # instance variable
        self.listB = []                          # instance variable
        for i in range(1):
            self.listA.append('a')
            self.listB.append('b')

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

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • True, but I was trying to figure out how to do that. I appreciate your help! Another StackOverflow question helped too discussing the __init__ method for a class to declare class level variables. – user650658 Jun 26 '12 at 22:38