Python Static Method
Introduction to methods that are majorly used in Python:
- Class Methods
- Instance Methods
- Static Methods
Before going to the in-depth concept of Static Methods, let us briefly discuss Class Methods and Instance Methods.
Class Methods
The methods that are bound to their respective class are known as Class Methods. Class Methods can access the state of their class and can make modifications if required.
Instance Methods
The methods that are bound to a particular instance of an object are known as Instance Methods. Instance methods can access the state of their particular object and make modifications if required.
Static Methods
Static methods are completely different from Class methods and Instance methods. They can neither access the state of classes nor the state of objects. Static methods are generally used to group similar functions which share a logical relationship.
Static methods are used when no objects are required to relate to the class. It is a method that is bound to the respective class but cannot access the state of the class. Static methods do not allow " self " argument. So, the " self " argument is not involved within the parenthesis of the method as a parameter.
Static methods can be called by using the class name but not the object name. They are defined within/inside the class, but they cannot access the instances of the class. Static methods are defined by using a decorator " @staticmethod ". Let us understand everything about the Static methods with the syntax followed by an example.
Syntax of Static methods:
class ClassName:
# declaration of the static method with a decorator @staticmethod
@staticmethod
# considering the name of the static method as static_method
def static_method(arg1, arg2, arg3,......, argn):
# statements that are supposed to be
# given within the static method
Example program defining Static methods :
class Mathematics:
# declaration of the static method with a decorator @staticmethod
@staticmethod
# defining a method for performing the addition of two numbers
def addition(x,y):
return x+y
# declaration of static method with a decorator @staticmethod
@staticmethod
# defining a method for performing subtraction between two numbers
def subtraction(x,y):
return x-y
# declaration of static method with a decorator @staticmethod
@staticmethod
# defining a method for performing modulo subtraction of two numbers
def moduloSubtraction(x,y):
if(x>y):
return x-y
else:
return y-x
# declaration of static method with a decorator @staticmethod
@staticmethod
# defining a method for performing division of two numbers
def division(x,y):
return x/y
# declaration of the static method with a decorator
# @staticmethod
@staticmethod
# defining a method for performing multiplication of two # numbers
def multiplication(x,y):
return x*y
# declaration of the static method with a decorator #@staticmethod
@staticmethod
# defining a method for resulting power of numbers
# this method gives the first number to the power of the #second number
def power(x,y):
return pow(x,y)
# taking inputs with the variables num1 and num2
num1 = int(input("Enter the value of the first number: "))
num2 = int(input("Enter the value of the second number: "))
# printing the statements using the static methods
print("The addition of the given numbers is equal to: ")
print(Mathematics.addition(num1,num2))
print("Subtraction between the given numbers is equal to: ")
print(Mathematics.subtraction(num1,num2))
print("Modulo subtraction of the given numbers is equal to: ")
print(Mathematics.moduloSubtraction(num1,num2))
print("Division of the given numbers is equal to: ")
print(Mathematics.division(num1,num2))
print("Multiplication of the given two numbers is equal to: ")
print(Mathematics.multiplication(num1,num2))
print("The power of first number to the second number is equal to: ")
print(Mathematics.power(num1,num2))
Output :
Enter the value of the first number:
3
Enter the value of the second number:
9
The addition of the given numbers is equal to:
12
Subtraction between the given numbers is equal to:
-6
Modulo subtraction of the given numbers is equal to:
6
Division of the given numbers is equal to:
0.3333333333333333
Multiplication of the given two numbers is equal to:
27
The power of the first number to the second number is equal to:
19683
Explanation :
The Static methods have been declared within the class " Mathematics ". Every static method has its purpose, and different operations are being performed within each method. The main motive of the static method is the same thing. Performing different operations within the same class and not being more accessible. Every static method has its return value related to the operation being performed in that particular method. After declaring the methods, we have taken the input of two numbers with variables " num1 " and " num2 " and allowed to print the results of all methods considering num1 and num2 as parameter values.