The static variable and static technique are the broadly used programming ideas in a variety of languages such as C++, PHP, Java, etc. These variables and methods belong to the type and the objects. In this section, we will examine how we create static variables and methods in Python.
Python Static Variable
When we declare a variable inside a class, however outside the method, it is known as a static or classification variable. It can be referred to as at once from a class but not via the situations of a class. However, the static variables are quite special from the different member, and it does no longer conflict with the equal variable identify in the Python program.
Let’s consider a software to show the use of static variables in the Python.
Static.py
class Employee: # create Employee class name
dept = 'Information technology' # define class variable
def __init__(self, name, id):
self.name = name # instance variable
self.id = id # instance variable
# Define the objects of Employee class
emp1 = Employee('John', 'E101')
emp2 = Employee('Marcus', 'E105')
print (emp1.dept)
print (emp2.dept)
print (emp1.name)
print (emp2.name)
print (emp1.id)
print (emp2.id)
# Access class variable using the class name
print (Employee.dept) # print the department
# change the department of particular instance
emp1.dept = 'Networking'
print (emp1.dept)
print (emp2.dept)
# change the department for all instances of the class
Employee.dept = 'Database Administration'
print (emp1.dept)
print (emp2.dept)
Output:
Information technology
Information technology
John
Marcus
E101
E105
Information technology
Networking
Information technology
Networking
Database Administration
In the above example, the dept is a type variable defined backyard of the classification strategies and internal the type definition. Where name and identification are the occasion variable that is described internal a method.
Access the static variable the usage of the identical type object
We can at once access a static variable in Python the usage of the identical category object with a dot operator.
Let’s consider a program to get admission to the static variable in Python the use of the equal category object.
staticVar.py
class Car:
# define the class variable or static variable of class Car
num = 7
msg = 'This is a good Car.'
# create the object of the class
obj = Car()
# Access a static variable num using the class name with a dot operator.
print ("Lucky No.", Car.num)
print (Car.msg)
Output:
Lucky No. 7
This is a good Car
Static Method
Python has a static approach that belongs to the class. It is just like a static variable that bounds to the classification as a substitute than the class’s object. A static method can be known as without creating an object for the class. It skill we can without delay call the static approach with the reference of the classification name. Furthermore, a static method is restricted with a class; therefore it can’t exchange the nation of an object.
Features of static methods
Following are the features of the static method:
A static method in Python related to the class.
It can be called directly from the class by reference to a class name.
It cannot access the class attributes in the Python program.
It is bound only to the class. So it cannot modify the state of the object
It is also used to divide the utility methods for the class.
It can only be defined inside a class but not to the objects of the class.
All the objects of the class share only one copy of the static method.
There are the two approaches to define a static approach in Python:
Using the staticmethod() Method
Using the @staticmethod Decorator
Using the staticmethod() Method
A staticmethod() is a built-in function in Python that is used to return a given characteristic as a static method.
Syntax:
staticmethod (function)
A staticmethod() takes a single parameter. Where the surpassed parameter is a feature that wishes to be converted to a static method.
Let’s reflect onconsideration on a software to create a characteristic as a static technique the use of staticmethod() in Python.
staticMethod.py
class Marks:
def Math_num(a, b): # define the static Math_num() function
return a + b
def Sci_num(a, b): # define the static Sci_num() function
return a +b
def Eng_num(a, b): # define the static Eng_num() function
return a +b
# create Math_num as static method
Marks.Math_num = staticmethod(Marks.Math_num)
# print the total marks in Maths
print (" Total Marks in Maths" , Marks.Math_num(64, 28))
# create Sci_num as static method
Marks.Sci_num = staticmethod(Marks.Sci_num)
# print the total marks in Science
print (" Total Marks in Science" , Marks.Sci_num(70, 25))
# create Eng_num as static method
Marks.Eng_num = staticmethod(Marks.Eng_num)
# print the total marks in English
print (" Total Marks in English" , Marks.Eng_num(65, 30))
Output:
Total Marks in Maths 92
Total Marks in Science 95
Total Marks in English 95
In the above program, we declared the Math_num method, Sci_num method, and Eng_num technique as static approach outdoor the classification using staticmethod() function. After that, we can call the static technique at once the use of the category identify Marks.
Using the @staticmethod Decorator
A @staticmethod is an inbuilt decorator that defines the static technique inner the class. It does now not receive any argument as a reference to a class instance or a category calling the static method itself.
Syntax:
class Abc:
@staticmethod
def function_name (arg1, arg2, ?):
# Statement to be executed
Returns: a static method for function function_name
Note: A @staticmethod is a cutting-edge approach to outline a method as a static method, and most of the programmer uses this approach in Python programming.
Let’s create a software to outline the static technique the use of the @staticmethod decorator in Python.
staticFun.py
class Marks:
@staticmethod
def Math_num(a, b): # define the static Math_num() function
return a + b
@staticmethod
def Sci_num(a, b): # define the static Sci_num() function
return a +b
@staticmethod
def Eng_num(a, b): # define the static Eng_num() function
return a +b
# print the total marks in Maths
print (" Total Marks in Maths" , Marks.Math_num(64, 28))
# print the total marks in Science
print (" Total Marks in Science" , Marks.Sci_num(70, 25))
# print the total marks in English
print (" Total Marks in English" , Marks.Eng_num(65, 30))
Output:
Total Marks in Maths 92
Total Marks in Science 95
Total Marks in English 95
Access a static method the usage of the equal classification object
Consider a application to get entry to the static method of the type the use of the @staticmethod in Python.
Test.py
class Test:
# define a static method using the @staticmethod decorator in Python.
@staticmethod
def beg():
print ("Welcome to the World!! ")
# create an object of the class Test
obj = Test()
# call the static method
obj.beg()
Output:
Welcome to the World!!
Function returns a value using the static method
Let’s write a software to return a value using the @staticmethod in Python.
Static_method.py
class Person:
@staticmethod
def Age (age):
if (age <= 18): # check whether the Person is eligible to vote or not.
print ("The person is not eligible to vote.")
else:
print ("The person is eligible to vote.")
Person.Age(17)
Output:
The person is not eligible to vote.
Leave a Review