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.
A class can have two kinds of attributes:
- 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 the same for every object of a class, it should be a class variable. Class variables are sometimes called static variables, as in languages like Java.
All instances of a class share class variables. Unlike an instance variable, a class variable has the same value for every object.
In Python, class variables are declared when a class is defined, directly in the class body and not inside any method. Because of this, only one copy of each class variable exists, and it is shared between all objects of the class.
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, a class variable is the better choice, 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, class variables are placed right below the class header, before the constructor 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, you can access a class variable in the following places:
- Access inside the constructor by using either
selfparameter or class name. - Inside an instance method, by using either
selfor the 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 both the class name and the self parameter inside the constructor.
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, you assign a value to a class variable in the class definition. However, you can change its value later, either inside the class or from outside of it.
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 only, and it shadows the class variable. s2 and the class itself still see ‘ABC School’. So, always use the class name to modify a class variable.
Class Variable vs. Instance variables
Let’s compare instance variables and class variables side by side.
To recap the two kinds of attributes:
- 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 the
__init__()method. Class variables are shared by all instances of a class.
Read More: Instance variables in Python with Examples. The following table shows the differences between instance and class variables.
| 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 usually created inside the constructor, i.e., the __init__() method, using self. | 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 class definition is executed, before any object exists. |
| Changes made to these variables through one object will not reflect in another object. | Changes made to the class variable through the class name are reflected 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, the child class can read all the class variables and methods of the base class.
But be careful when assigning: Student.course = ... creates a new class variable on the child class Student, and the parent’s value in Course.course stays unchanged. To change the value for the parent and every class that inherits it, assign through the parent class name: Course.course = ....
Example
Output
Before Student name: Emma Course Name: Python Now Student name: Emma Course Name: Machine Learning Parent class course: Python
What if both child and parent classes have the same class variable name? In this case, the child’s class variable overrides (shadows) the parent’s for the child class and its objects. The parent class keeps its own value, as the following example shows.
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
Use class variables carefully, because all objects share the same copy. A common mistake is to “modify” a class variable through an object. This doesn’t change the shared value at all. It silently creates an instance variable for that one object.
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 variables club and sport can be read by any object.
When each object assigned to a class variable, Python created a new instance variable with the same name for that particular object, which shadows the class variable.
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 access these names through p1 or p2, you get the instance variable, not the actual class variable value.
To avoid this, always modify a class variable using the class name, so that all objects get the updated value:
Be careful with mutable class variables
If a class variable holds a mutable object, such as a list or dictionary, changing that object through any instance changes it for all instances. No new instance variable is created, because you’re modifying the shared object, not assigning a new value.
Practical example: count objects with a class variable
A common use of a class variable is to keep data about the class as a whole, such as how many objects have been created. Increase the counter through the class name in the constructor:
Tip: Inside a method, you can also use type(self).count instead of the class name, or a class method with cls.count. These work correctly with subclasses too.
Summary
- A class variable is defined in the class body, outside any method, and is shared by all objects of the class.
- You can read it using the class name or an object, but you should modify it only using the class name.
- Assigning to a class variable through an object creates an instance variable that shadows it for that object only.
- In inheritance, a child class can read the parent’s class variables. Assigning through the child’s name creates a separate variable on the child.
- Avoid mutable class variables (lists, dicts) for per-object data. Use instance variables instead.
