01 Jun Class or Static Variables in Python
Class or Static Variables in Python
In this tutorial, we’ll discuss class or static variables in Python. Class or Static variables are the variables that belong to a class and not objects. We can also say they are shared amongst objects of the class. Therefore, all variables assigned a value in the class declaration are called class variables, and variables given inside the class methods are referred to as instance variables.
Whenever we have a class, all class objects will share the static variable. This static variables can be accessed either by class name or through object reference such as ClassName.static_variable_name
.
Let’s check the examples below to understand how this is done;
Example1
There is an Employee class with a class variable known as role in this example. We assigned a variable to our class variable called “Software Engineer”. Next, we defined a class constructor which accepts two new arguments. We then created our instance variable(_id and name). After this, we created 2 different objects for the Employee class. Through this object, we could access our static/class variable “role”. We were also able to call each of the instance variables using each of the individual objects created.
1 class Employee: 2 # Here is a Class Variable 3 role = 'Software Engineer' 4 def __init__(self,_id,name): 5 #this is an Instance Variable 6 self._id= _id 7 #this is an Instance Variable 8 self.name = name 9 10 11 # our objects of the Employee class 12 a = Employee(1, 'Adams') 13 b = Employee(2, 'Janes') 14 15 print(a.role) 16 print(b.role) 17 print(a._id) 18 print(b._id) 19 print(a.name) 20 print(b.name) 21 print(Employee.role)
Output
1 Software Engineer 2 Software Engineer 3 1 4 2 5 Adams 6 Janes 7 Software Engineer
We can change the value in a class variable by using class object but mind you; this doesn’t affect another object of the class that might just be created. Let’s see the demonstration below;
1 a.role = 'Data Engineer' 2 print(a.role) 3 print(b.role) 4 print(Employee.role)
Here, we use the
a object to change the value in the role(our class variable). We observe that this code won’t affect the second object and the
main class variable whenever we run this code. The reason is that this occurs at the
level of that particular object. We generated the output below;
Output
1 Data Engineer 2 Software Engineer 3 Software Engineer
Note: You can access the class variable by using either the class name or the object reference, just as seen below;
1 print(a.role) 2 print(Employee.role)
Output
1 Software Engineer 2 Software Engineer
Let’s check another example below on class or static variable;
Example 2
Here is another example on how to use the class or static variable. In this example, we demonstrated using a particular animals class called carnivorous. We defined our static variable in our class and also created our constructor which has an argument called name. We defined another new method used for printing all the objects of the animal class we created just as seen below;
1 class Animals: 2 # class or static variable 3 category = 'carnivorous' 4 5 def __init__(self, name): 6 # instance variable 7 self.name = name 8 9 10 # method to show data 11 def show(self): 12 # accessing class variable 13 print(self.name) 14 15 a = Animals('Lion.') 16 b = Animals('Wolf.') 17 c = Animals('Leopard.') 18 d = Animals('Hyena.') 19 e = Animals('Polar Bear.') 20 21 print('These animals belong to the class: ', Animals.category) 22 print('These animals are:') 23 a.show() 24 b.show() 25 c.show() 26 d.show() 27 e.show()
Output
We generated the output below;
1 These animals belong to the class: carnivorous 2 These animals are: 3 Lion. 4 Wolf. 5 Leopard. 6 Hyena. 7 Polar Bear.
Hope you enjoyed this post. Thanks for reading.
No Comments