What is a Static Variable in Python?
Imagine walking into a bakery and seeing a wide assortment of freshly-baked cakes over there. You see red-velvet, black-forest, chocolate-truffle, butterscotch cakes, and many more varieties of cakes. While there are different types of cakes, they have one common connection linking them: they are all cakes.
So, talking about our bakery example, the class containing the cakes may have a variable 'cake' associated with it. This variable is known as a static variable. In Python programming, a static variable is said to be a class variable that is common to all class members. A static variable is declared within that class but outside the methods within the given class.
Non-static variables are those variables that are associated with the objects or methods declared within the class. They are not the same for all the objects in a given class and differ, as opposed to static variables that remain constant for a given class. The flavors and the prices are non-static variables associated with different cake objects and vary accordingly.
If you are unfamiliar with classes in python, check out this insightful article before reading about static variables.
Syntax:
The output for the code mentioned above will be 2. Within the class * 'Demo'*, a static variable 'i' is defined.
Example of Static Variables
Let us look at the bakery example here to understand static variables. In this example, the class 'Bakery' has the class/static variable 'type' associated with it. This static variable holds the value 'cake'. This means that all the objects created in the class 'Bakery' will be cakes. The static variable is responsible for categorizing all the objects in the 'Bakery' with the same 'type'. 'Flavor' and 'price' are the instance variables which vary per object.
Code:
Output:
Explanation:
The objects declared in the class include a Butterscotch cake of Rs 300 stored in 'a' and a Chocolate-Truffle Cake costing Rs 250 stored in 'b'.
How to Access Static Variables from inside a Class?
Static variables can be accessed either from within or outside a class. In the example below, we are updating the number of cakes after each iteration.
While accessing the noOfCakes static variable can either be accessed by using the argument 'cls' or by simply using the class name.
Code:
Output:
Explanation:
The static variables could be accessed from inside a class either by using the classname('Cake' or by using 'cls')
- cls.ClassVariable
- ClassName.ClassVariable
The value of the cake stored in the static variable is updated after each step.
How to Access Static Variables from Outside a Class?
Static variables can also be accessed from outside a given class. This could be done by either using objects or class names.
Code:
Output:
Explanation:
In the above code, it could be seen that outside a class, static variables can be accessed in two ways.
- Using objects (obj.ClassVariable)
- Using class name (ClassName.ClassVariable)
In the first two print statements, static variables have been accessed and printed by using the c1 and c2 objects. In the third statement, the static variable has been accessed by using the class name 'Cake'.
Conclusion
- Static variables are those variables that are declared within a given class but outside the objects in the class.
- Class or static variables can be referred through a by class, but not through an instance directly.
- Python doesn't require users to use the 'static' keyword before declaring static variables.
- Static variables can be accessed either from outside the class or from within the class.
- Static variables could be accessed either by using class names or by using objects.