In Object-oriented programming, when we design a class, we use instance variables and class variables. Show In Class, attributes can be defined into two parts:
After reading this article, you’ll learn:
Table of contentsWhat is an Class Variable in Python?If the value of a variable is not varied from object to object, such types of variables are called class variables or static variables. Class variables are shared by all instances of a class. 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 methods of a class because of this only one copy of the static variable will be created and shared between all objects of the class. For example, in 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 as the school name is the same for all students. So instead of maintaining the 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. Understand Class VariablesCreate Class VariablesA class variable is declared inside of class, but outside of any instance method or Emma 10 ABC School Jessa 20 ABC School9 method. 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 1 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, Python tuple, and Python dictionary as a class variable. Accessing Class VariablesWe can access static variables either by class name or by object reference, but it is recommended to use the class name. In Python, we can access the class variable in the following places
Example 1: Access Class Variable in the constructor
Output ABC School ABC School 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 In this example, we accessed the class variable 1 using class name and a 2 keyword inside a method.Modify Class VariablesGenerally, 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: We should change the class variable’s value using the class name only. 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, and this variable shadows the class variables. So always use the class name to modify the class variable. Class Variable vs Instance variablesThe following table shows the difference between the instance variable and the class variable. In Python, properties can be defined into two parts:
Read More: Instance variables in Python with Examples Instance VariableClass VariableInstance variables are not shared by objects. Every object has its own copy of the instance attributeClass variables are shared by all instances.Instance variables are declared inside the constructor i.e., the Emma 10 ABC School Jessa 20 ABC School9 method.Class variables are declared inside the class definition but outside any of the instance methods and constructors.It is 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.Class Variables vs. Instance Variables Example: Let’s see the example to create a class variable and instance variable. Emma 10 ABC School Jessa 20 ABC School0 Output: Emma 10 ABC School Jessa 20 ABC School1 Class Variables In InheritanceAs 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 Emma 10 ABC School Jessa 20 ABC School2 Output Emma 10 ABC School Jessa 20 ABC School3 What if both child class and parent class has the same class variable name. In this case, the child class will not inherit the class variable of a base class. So it is recommended to create a separate class variable for child class instead of inheriting the base class variable. Example: Emma 10 ABC School Jessa 20 ABC School4 Output: Emma 10 ABC School Jessa 20 ABC School5 Wrong Use of Class VariablesIn Python, we should properly use the class variable because all objects share the same copy. Thus, if one of the objects modifies the value of a class variable, then all objects start referring to the fresh copy. For example, Example Emma 10 ABC School Jessa 20 ABC School6 Output Emma 10 ABC School Jessa 20 ABC School7 In the above example, the instance variable 7 is unique for each player. The class variable 8 and 9 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 ABC School ABC School0 new instance variable ABC School ABC School1 gets created, and for object ABC School ABC School2 new instance variable 9 gets created.So when you try to access the class variable using the ABC School ABC School0 or ABC School ABC School2 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 |