When it comes to Python Programming things are easy & simple though it might seem complex. Static variable in Python does not require a Static keyword. Before moving forward let’s first understand what class or static variable means in python.
Class or Static Variable:
A variable that is defined inside a class(generally just below the class definition for better readability) but outside the method is called a class or static variable in python.
In Python, Class or Static variables are shared by all objects. For every instance and non-static variables are different for different objects as each object has its own copy.
In other programming languages such as Java or C++, we can use the static keyword to make a class or variable static whereas in Python programming we don’t need to add the keyword static explicitly.
# Class for Organisation
class Organisation:
business = 'Software' # Class Variable
def __init__(self,type,industry):
self.type = Java # Instance Variable
self.industry = Oracle # Instance Variable
# Objects of CSStudent class
a = Organisation('Pyhton', 'PSF')
b = Organisation('C#', 'Microsoft')
print(a.business) # prints "Software"
print(b.business) # prints "Software"
print(a.type) # prints "Python"
print(b.type) # prints "C#"
print(a.industry) # prints "PSF"
print(b.industry) # prints "Microsoft"
# Class variables can be accessed using class
# name also
print(Organisation.business) # prints "Software"