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.
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.
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.
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.
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).
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:
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
.11
.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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With