In Python, class variables (also known as class attributes) are shared across all instances (objects) of a class. They belong to the class itself, not to any specific instance.
In Object-oriented programming, we use instance and class variables to design a Class.
In Class, attributes can be defined into two parts:
- Instance variables: If the value of a variable varies from object to object, then such variables are called instance variables.
- Class Variables: A class variable is a variable that is declared inside of a Class but outside of any instance method or
__init__()
method.
After reading this article, you’ll learn:
- How to create and access class variables
- Modify values of class variables
- Instance variable vs. class variables
- The behavior of a class variable in inheritance
Table of contents
What is Class Variable in Python?
If the value of a variable is not varied from object to object, such types of variables are called class or static variables.
All instances of a class share class variables. Unlike instance variable, the value of a class variable is not varied from object to object,
In Python, class variables are declared when a class is being constructed. They are not defined inside any method of a class. Because of this, only one copy of the static variable will be created and shared between all class objects.
For example, in the Student class, we can have different instance variables such as name and roll number because each student’s name and roll number are different.
But, if we want to include the school name in the student class, we must use the class variable instead of an instance variable because the school name is the same for all students. So, instead of maintaining a separate copy in each object, we can create a class variable that will hold the school name so all students (objects) can share it.
We can add any number of class variables in a class.
Create Class Variables
A class variable is declared inside of a class, but outside of any instance method or __init__()
method.
You can use the class name or the instance to access a class variable.
By convention, typically, it is placed right below the class header and before the constructor method and other methods.
Example:
Output
Emma 10 ABC School Jessa 20 ABC School
In the above example, we created the class variable school_name
and accessed it using the object and class name.
Note: Like regular variables, class variables can store data of any type. We can use Python list, tuple, and dictionary as a class variable.
Accessing Class Variables
We can access class variables by class name or object reference, but it is recommended to use the class name.
In Python, we can access the class variable in the following places
- Access inside the constructor by using either
self
parameter or class name. - Access class variable inside instance method by using either self of class name
- Access from outside of class by using either object reference or class name.
Example 1: Access Class Variable in the constructor
Output
ABC School ABC School
In this example, we accessed the class variable school_name
using class name and a self
keyword inside a method.
Example 2: Access Class Variable in Instance method and outside class
Output
Inside instance method Emma 10 ABC School ABC School Outside class ABC School ABC School
Modify Class Variables
Generally, we assign value to a class variable inside the class declaration. However, we can change the value of the class variable either in the class or outside of class.
Note: It is recommended to change the class variable’s value using the class name.
If you modify a class variable using an instance, it doesn’t change the class variable itself. Instead, it creates an instance variable with the same name that shadows (or hides) the class variable for that instance.
Example:
Output:
Before Emma 10 ABC School After Emma 10 XYZ School
Note:
It is best practice to use a class name to change the value of a class variable. Because if we try to change the class variable’s value by using an object, a new instance variable is created for that particular object, which shadows the class variables.
Example:
Output:
Before Emma 10 ABC School Jessa 20 ABC School After Emma 10 PQR School Jessa 20 ABC School
A new instance variable is created for the s1 object, which shadows the class variables. So, always use the class name to modify the class variable.
Class Variable vs. Instance variables
The following table shows the difference between the instance and class variables.
In Python, properties can be defined into two parts:
- Instance variables: The instance variable’s value varies from object to object. Objects do not share instance variables. Every object has its copy of the instance attribute.
- Class Variables: A class variable is a variable that is declared inside of a class but outside of any instance method or
init()
method. Class variables are shared by all instances of a class.
Read More: Instance variables in Python with Examples
Instance Variable | Class Variable |
---|---|
Instance variables are not shared by objects. Every object has its own copy of the instance attribute. | Class variables are shared by all instances. |
Instance variables are declared inside the constructor i.e., the __init__() method. | Class variables are declared inside the class definition but outside any of the instance methods and constructors. |
It gets created when an instance of the class is created. | It is created when the program begins to execute. |
Changes made to these variables through one object will not reflect in another object. | Changes made in the class variable will reflect in all objects. |
Example:
Let’s see the example to create a class variable and instance variable.
Output:
x1 2500 BMW
Class Variables In Inheritance
As you know, only one copy of the class variable will be created and shared between all objects of that class.
When we use inheritance, all variables and methods of the base class are available to the child class. In such cases, We can also change the value of the parent class’s class variable in the child class.
We can use the parent class or child class name to change the value of a parent class’s class variable in the child class.
Example
Output
Before Student name: Emma Course Name: Python Now Student name: Emma Course Name: Machine Learning
What if both child and parent classes have the same class variable name? In this case, the child class will not inherit the class variable of a base class. So, creating a separate class variable for the child class is recommended instead of inheriting the base class variable.
Example:
Output:
Before Student name: Emma Course Name: SQL Now Student name: Emma Course Name: Machine Learning Parent Class Course Name: Python
Wrong Use of Class Variables
We should use the class variable carefully in Python because all objects share the exact copy. Thus, if one of the objects modifies the value of a class variable, then all objects start referring to the fresh copy.
Example
Output
Player : Name: John Club: FC Sports: Football Player : Name: Emma Club: Chelsea Sports: Tennis Club: Chelsea Sport: Football
In the above example, the instance variable name
is unique for each player. The class variable team
and sport
can be accessed and modified by any object.
Because both objects modified the class variable, a new instance variable is created for that particular object with the same name as the class variable, which shadows the class variables.
In our case, for object p1
new instance variable club
gets created, and for object p2
new instance variable sport
gets created.
So when you try to access the class variable using the p1
or p2
object, it will not return the actual class variable value.
To avoid this, always modify the class variable value using the class name so that all objects gets the updated value. Like this