Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Class variables and Instance variables

Tags:

python

oop

class

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

While playing around, I found that this code which gives the output [1]:

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

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

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

However, this code gives 10 as the output, when in my opinion it should be 11:

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

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

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

Why A class variable is not updated when I run x.add()? I am not very experienced in programming, so please excuse me.

like image 711
MayankJain Avatar asked Nov 03 '13 14:11

MayankJain


People also ask

What is the difference between class and instance?

A class is a blueprint which you use to create objects. An object is an instance of a class - it's a concrete 'thing' that you made using a specific class. So, 'object' and 'instance' are the same thing, but the word 'instance' indicates the relationship of an object to its class.

What is the difference between class variables and instance variables in Python?

A class variable is a variable that defines a particular property or attribute for a class. An instance variable is a variable whose value is specified to the Instance and shared among different instances. We can share these variables between class and its subclasses. We cannot share these variables between classes.

Is instance variable same as class variable in Java?

Class variables are common to all objects of a class, if any changes are made to these variables through object, it will reflect in other objects as well. Instance variables are declared without static keyword. Instance variables can be used only via object reference.

What is the difference between instance variable and field?

A static field (also called class variable) is one variable, which is shared by all instances. So, concluding: an instance variable is always a field, but not all fields are instance variables (they can be class variables as well).


1 Answers

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.)

like image 87
millimoose Avatar answered Sep 20 '22 20:09

millimoose