Class Variable in Python

In this class, we discuss Class Variable in Python.

For Complete YouTube Video: Click Here

Class Variable

The reader should have prior knowledge of class and instance variables. Click here.

Take an example and understand the concept of the class variable.

class test:
    j=20 # class variable
    def __init__(self,l):
        self.instvar=l # instance variable
    def f(self):
        print(self.instvar+test.j)
    def f1(self):
        test.j=test.j+5
        print(test.j)
print(test.j)
x=test(30)
y=test(80)
x.f()
x.f1()
print(y.j)#access class variable using object name

Output:
20
50
25
25

In the program given above. We define a class Test.

In the Test class, we had a class variable j. variable j is defined in the class. We call class variable.

The class variables are assigned memory location during the compile time.

Assume the memory space assigned to the class variable is at location 1000.

class variable j is assigned a value of 20.

In the Test class, we have a constructor and methods f and f1.

The instance variable instvar is defined in the constructor.

We are accessing the class variable j using the class name. Test.j

After displaying the class variable, we defined an object x to the Test class. And the instance variable is given a value of 30.

A separate memory is allocated to object x. For example, assume memory allocated at location 5000.

One more object is defined with variable y, and the instance variable is assigned a value of 80.

The memory allocated to object y is at location 6000.

From the above discussion, we understand each object has its instance variables.

A class variable is assigned only once.

Point to understand

A class variable is global to all the objects.

Any object can access the class variable using the class name. This example is given in the functions f and f1.

Function f is using both an instance variable and a class variable.

Function f1 is modifying the class variable. 

Once a class variable is modified. The modified value is available to all the objects because it is global to all objects.

In the last line of the program, we are displaying class variables using the object name.

Yes, we can call class variable using object name.

When we call class variable using object name, it will check the variable at instance variable memory location.

Suppose the variable is not found at instance variables. Then check in class variables because class variables are global to all objects.

The class variable concept is a similar concept to local and global variables in functions.

Analyze the program given below for a better understanding of the concept of the class variable.

Program

class test:
    j=20 # class variable
    def __init__(self,l):
        self.instvar=l # instance variable
    def f(self):
        print(self.instvar+test.j)
    def f1(self):
        test.j=test.j+5
        print(test.j)
print(test.j)
x=test(30)
y=test(80)
x.f()
x.f1()
print(y.j)
x.j=200
print(x.j)
print(y.j)
print(test.j)

Output:
20
50
25
25
200
25
25