Nearby lessons
104 of 159Python - Static Variables
- Understand what a static variable is and how it is shared
- Know the six places where static variables can be declared
- Access static variables using the class name or an object reference
- Modify and delete static variables
- Distinguish between static variables and instance variables
Static Variables
Static variables are variables whose values are common to all objects of a class.
Unlike instance variables, static variables are created only once and are shared by every object.
A static variable belongs to the class, not to individual objects. Because there is only one copy, any modification is visible to all objects.
Static variables are also known as Class Variables.
Characteristics of Static Variables
- Belong to the class rather than individual objects.
- Only one copy exists in memory.
- Shared among all objects of the class.
- Every object accesses the same value.
- Memory efficient because duplicate copies are not created.
- Can be accessed using either the class name or an object reference.
- Changing the value affects all objects.
Syntax
Static variables are declared directly inside the class but outside any method.
Explanation
Here, school is a static (class) variable shared by every student, while name is an instance variable that is different for every student.
Instance Variable vs Static Variable
| Feature | Instance Variable | Static Variable |
|---|---|---|
| Belongs To | Object | Class |
| Copies Created | One per object | Only one |
| Memory Usage | Higher | Lower |
| Values | Can differ for each object | Same for all objects |
| Access Using | Object Reference | Class Name or Object |
| Modification Effect | Only that object | All objects |
Various Places to Declare Static Variables
Static variables can be declared or modified in several places within a Python program:
- Inside the Class (Direct Declaration)
- Inside the Constructor
- Inside an Instance Method
- Inside a Class Method
- Inside a Static Method
- Outside the Class
Each technique updates the class variable, which is shared by every object.
Program: Declaring Static Variables in All Six Places
Explanation
collegeis declared directly inside the class and then updated inside the class method.cityis declared inside the constructor.countryis declared inside an instance method.stateis declared inside a static method.universityis declared outside the class usingStudent.university = "GTU".
Accessing Static Variables
Since static variables belong to the class, they can be accessed in two ways.
Explanation
Although both approaches work, using the class name (Student.college) is considered the best practice because it clearly indicates that the variable belongs to the class.
Modifying Static Variables
Static variables should normally be modified using the class name.
Deleting Static Variables
Static variables can be deleted using the del keyword.
Program: Banking Application Using a Static Variable
This program shows a real-world use of static and instance variables together.
Explanation
The static variable bankName is shared by every customer, so every display() call prints the same bank name.
The instance variables name and balance are different for every customer.
Notice that when withdraw() is called with an amount greater than the balance, the program displays "Insufficient Balance".
- A static variable belongs to the class and is shared by all objects
- Only one copy of a static variable exists in memory
- It is recommended to access static variables using the class name
- Modifying a static variable affects every object of the class
- Static variables can be declared inside the class, constructor, instance method, class method, static method, or outside the class