Static Variables in Python
What is a Static Variable?
The variable that remains with a constant value throughout the program or throughout the class is known as a " Static Variable ". Static variables are not instantiated, i.e., they are not the created objects but declared variables.
Syntax of Static variables:
class ClassName:
# static variable is being created immediately after the class declaration
static_variable = "giving some value to the static variable "
# the static variable is being accessed and printed
print(ClassName.static_variable)
Static variables are also known as Class variables. A class may contain both Static variables and Non – Static members.
Let us discuss everything about Static variables with an example.
General Example:
Let us consider all humans being represented as a class " Human ". The class " Human " may have a static variable; let it be " x " where x = " Human ". The class may also consist of non – static variables like “gender“, ” name “, “ place_of_residence “, " mother_tongue ", etc. Here, x is considered to be the common part among all humans, i.e., they are humans.
So, x is a fixed variable within the entire program. Hence, the variable " x " is known as a Static variable, whereas the variables "gender", " name ", " place_of_residence ", and " mother_tongue " differ from person to person. So, they are known as Non – static variables.
In python, we generally do not use the " static " keyword to determine static variables. The variables declared immediately after the class declaration are known to be " Static Variables " or " Class Variables ". The variables declared within the method body are known as " Instance variables ".
Let us see an example program where static variables are declared.
Example program :
#class HumanSpecies is being declared for all humans
class HumanSpecies:
species = 'Humans'
def __init__(self,gender,name,place_of_residence,mother_tongue,country):
self.gender= gender
self.name= name
self.place_of_residence= place_of_residence
self.mother_tongue= mother_tongue
self.country= country
# creating objects of HumanSpecies class
obj1 = HumanSpecies("Female","Siri","Hyderabad","Telugu","India")
obj2 = HumanSpecies("Male","Kanishk","Mumbai","Hindi","India")
# printing the values using objects
print("The value of Static variable while accessing with object1 is equal to "+obj1.species)
print("The value of Static variable while accessing with object2 is equal to "+obj2.species)
print(obj1.name+" :")
print("My name is "+obj1.name+", and I am from "+obj1.place_of_residence)
print("I can speak "+obj1.mother_tongue+" very fluently.")
print(obj2.name+" :")
print("My name is "+obj2.name+", and I am from "+obj2.place_of_residence)
print("I can speak "+obj2.mother_tongue+" very fluently.")
#printing the value of species using class
print("The value of species before changing is "+HumanSpecies.species)
# as we know that the value of the static variable cannot be changed for all objects, let us try it by changing the value using a single object
obj1.species='Dog'
print("The changed value of static using object1 is "+obj1.species)
print("The value of a static variable while accessing with object2 is "+obj2.species)
The output of the program:
Static variable value while accessing with object1 is equal to Humans
Static variable value while accessing with object2 is equal to Humans
Siri :
My name is Siri, and I am from Hyderabad
I can speak Telugu very fluently.
Kanishk :
My name is Kanishk, and I am from Mumbai
I can speak Hindi very fluently.
The value of species before changing is Humans
The changed value of the static variable using object1 is Dog
The value of the static variable while accessing with object2 is Huma
Explanation:
Initially, the class “ HumanSpecies " is created and declared a static variable, i.e., " species ". The value of the static variable is taken as " Humans ". The non–static members, i.e., “ gender “, ” name “, “ place_of_residence “, “ mother_tongue “, and " country ", have been declared within the function. Two objects are created to access the class "HumanSpecies ”.
Using the objects, the value of the static variable is printed. All the members that have been considered are printed, and then the value of the static variable “ species “ has been modified using the object “ obj1 “. The value has been changed concerning the object " obj1 " but not to " obj2 ". So, when " obj2 " calls the static variable, the initial value " Humans " is printed.
Conclusion:
The value of the static variable cannot be changed unless it is changed using that particular class or an object created within the class.