Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I have already read many answers here on Stack Exchange like Python - why use "self" in a class? After reading these I understand that instance variables are unique to each instance of the class while the class variable is shared across all instances. While playing around I found that this code -

class A:
    x = []
    def add(self):
        self.x.append(1)

x = A()
y = A()
x.add()
print "Y's x:",y.x

does give the output [1]. However, this code -

class A:
    x = 10
    def add(self):
        self.x += 1

x = A()
y = A()
x.add()

print "Y's x:",y.x

gives the output as 10 when in my opinion it should be 11. Please excuse me if this is a very noobish questions but I am not very experienced in programming.

share|improve this question
    
Thanks @mbatchkarov , I don't know how I let that slip :) – MayankJain Nov 3 '13 at 14:21

1 Answer 1

up vote 7 down vote accepted

Class variables are shadowed by instance attribute. This means that when looking up an attribute, Python first looks in the instance, then in the class. Furthermore, setting a variable on an object (e.g. self) always creates an instance variable - it never changes the class variable.

This means that when, in your second example you do:

self.x += 1

which is (in this case, see footnote) equivalent to:

self.x = self.x + 1

what Python does is:

  1. Look up self.x. At that point, self doesn't have the instance attribute x, so the class attribute A.x is found, with the value 10.
  2. The RHS is evaluated, giving the result 11.
  3. This result is assigned to a new instance attribute x of self.

So below that, when you look up x.x, you get this new instance attribute that was created in add(). When looking up y.x, you still get the class attribute. To change the class attribute, you'd have to use A.x += 1 explicitly – the lookup only happens when reading the value of an attribute.


Your first example is a classical gotcha and the reason you shouldn't use class attributes as "default" values for instance attributes. When you call:

self.x.append(1)

there is no assignment to self.x taking place. (Changing the contents of a mutable object, like a list, is not the same as assignment.) Thus, no new instance attribute is added to x that would shadow it, and looking up x.x and y.x later on gives you the same list from the class attribute.


Note: In Python, x += y is not always equivalent to x = x + y. Python allows you to override the in-place operators separately from the normal ones for a type. This mostly makes sense for mutable objects, where the in-place version will directly change the contents without a reassignment of the LHS of the expression. However, immutable objects (such as numbers in your second example) do not override in-place operators. In that case, the statement does get evaluated as a regular addition and a reassignment, explaining the behaviour you see.

(I lifted the above from this SO answer, see there for more details.)

share|improve this answer
    
Great explanation! – aIKid Nov 3 '13 at 14:26
    
Actually, x += ... is not always equivalent to x = x + .... You can observe that if you change the code with the list class variabel to use self.x += [1] instead of self.x.append(1). – delnan Nov 3 '13 at 14:29
    
I'd quibble a bit about x += y being generally equivalent to x = x + y; __iadd__ and __add__ are different. – DSM Nov 3 '13 at 14:29
    
@DSM Hm. It seems to work for the purposes of this example, I'll poke at the docs to see if I can phrase it without that handwaving though. EDIT: Donezo. – millimoose Nov 3 '13 at 14:31
    
What do you mean you shouldn't use class attributes as "default" values for instance attributes ? – MayankJain Nov 3 '13 at 16:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.