Class vs Instance Variables

Distinction between class and instance variables explained.

Published in
3 min readJun 9, 2019

--

Variables are essentially symbols that stand in for a value we’re using in a program. Object-oriented programming allows for variables to be used at the class level or the instance level. The aim of this article is to have clear distinction between types of variables offered by Python’ object model and goes on to further discuss a peculiar behaviour that might lead to some surprising results, if neglected. So Let’s dig in!
As per Python’s object model, there are two kinds of data attributes on Python objects: class variables and instance variables.

Class Variables — Declared inside the class definition (but outside any of the instance methods). They are not tied to any particular object of the class, hence shared across all the objects of the class. Modifying a class variable affects all objects instance at the same time.

Instance Variable — Declared inside the constructor method of class (the __init__ method). They are tied to the particular object instance of the class, hence the contents of an instance variable are completely independent from one object instance to the other.

Let’s start with a short and easy-to digest example:

class Car:
wheels = 4 # <- Class variable
def __init__(self, name):
self.name = name # <- Instance variable

Above is the basic, no-frills Car class defined. Each instance of it will have class variable wheels along with the instance variable name. Let’s instantiate the class to access the variables.

>>> jag = Car('jaguar')
>>> fer = Car('ferrari')
>>> jag.name, fer.name
('jaguar', 'ferrari')
>>> jag.wheels, fer.wheels
(4, 4)
>>> Car.wheels
4

Accessing the instance variable name is pretty straight forward. However there is a little more flexibility when it comes to access the class variable. As above, we can access wheels via the object instance or the Class itself.

Also trying to access the name through the class will result in an AttributeError since instance variables are object specific and are created when __init__ constructor is invoked. This is the central distinction between the class and instance variables.

>>> Car.name
AttributeError: type object 'Car' has no attribute 'name'

Now, let’s assume for time being that our Jaguar car has 3 wheels. (yes, its stupid but bear with it!! 😅). To represent that in our code, we can modify the wheels variable.

>>> Car.wheels = 3

What we did above will make all cars with 3 wheels since we have modified a class variable, which will apply to all instances of Car class.

>>> jag.wheels, fer.wheels
(3, 3)

Therefore modifying a class variable on the class namespace affects all the instances of the class. Let’s roll back the change and modify the wheels variable using the jag object.

>>> Car.wheels = 4
>>> jag.wheels = 3

This will give us the output we desire.

>>> jag.wheels, fer.wheels, Car.wheels
(3, 4, 4)

Although we got the result we wanted, but what happened behind the scenes is a new wheels variable that has been added to jag object and this new variable shadows the class variable with same name, overriding and hiding it. We can access both the wheels variable as below.

>>> jag.wheels, jag.__class__.wheels
(3, 4)

Hence we can conclude that jag.wheels = 3 created a new instance variable with the same name as the class variable (wheels). This is an important concept to be aware of.
Making use of class- and instance-specific variables can ensure that our code adheres to the DRY principle to reduce repetition within code.
Knowing this I feel can be real handy and save a lot of hours of debugging one day. 😇
Refer to my this post to further read on various kind of methods (instance, class & static) in Python.

Conclusions

  • Class variables are shared across all objects while instance variables are for data unique to each instance.
  • Instance variable overrides the Class variables having same name which can accidentally introduce bugs or surprising behaviour in our code.

--

--

Sports Enthusiast | Senior Deep Learning Engineer. Python Blogger @ medium. Background in Machine Learning & Python. Linux and Vim Fan