×

Python Static Method

Introduction to methods that are majorly used in Python:

  1. Class Methods
  2. Instance Methods
  3. 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.


Related Topics

Python Assert

Python Assert Python provides an assert statement which is used to check the logical expression. If the given logical expression is true, then it precedes for the next line; otherwise, it raises an...

2 minutes read.

Python Parallel Processing

By performing more jobs concurrently, your software may complete more tasks in a shorter amount of time. These aid in solving major issues. The following subjects will be covered in...

2 minutes read.

How to implement classifiers in Python

What is classification? Classification is a type of supervised machine learning problem with a categorical target (response) variable. Given the known label in the training data, the classifier approximates a mapping...

4 minutes read.

How to assign values to variables in Python and other languages?

Python makes it simple to construct variables. The value to be stored in the variable should then be written after a suitable name for the variable and the equality sign....

3 minutes read.

Python Num2words

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Python Line Break

Introduction In this tutorial, you will learn line breaks in python.In Python, the new line character is used to indicate the start of a new line and the end of an...

5 minutes read.

_dict_ in Python

An unordered collection of data values known as a dictionary can be used in Python to store data values similar to a map. Dictionaries can also store a key: value...

6 minutes read.

Io stringio Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

7 minutes read.

Python Interpreter

In this tutorial, we will go through the basic knowledge about what an interpreter is and how we use it in the python programming language. It is one of the...

3 minutes read.

Python Logical Operators

In python, there are many operators which we use in our program. Operators are used in manipulating a particular value or operand. Logical operators are used mainly to evaluate an...

7 minutes read.

Python Unit Test Cheat String

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

What is Python 2

Python is a widely used high-level language. The initial work on developing python was begun in the late 1980s. In 1989, Guido Van Rossum started to work on it. Initially,...

3 minutes read.

OSError in Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

4 minutes read.

Python Comment Block

In this tutorial, we will see what comment blocks mean in Python. Further, we will see the commenting methods supported in Python. We will understand the topics deeply with the...

6 minutes read.

List Index out of Range Python for Loop

The List is generally used in Python to store multiple items or elements inside one single variable. The List is ordered in nature, the List can contain the elements inside...

3 minutes read.

Python Online Compiler

Compose, Run and Offer Python code internet involving OneCompiler's Python online compiler free of charge. It's one of the hearty, highlight-rich web-based compilers for python language, supporting both the adaptations, which...

3 minutes read.

How to make a firewall in Python?

Firewall: The firewall is a network which controls the incoming and outgoing network traffics of a monitor. It blocks the dataset based on the set of rules written in the security...

3 minutes read.

Self in Python

 “self" is neither a keyword nor has a special meaning in Python, but it has a place and a job to do in Object-oriented programming. When we create a class...

6 minutes read.

Python e-book free download

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. There are many sources to learn python. In this...

3 minutes read.

Python | a += b is not always a = a + b

a += b in Python doesn't always behave the same way as a = a + b; the same operands can produce different outcomes depending on the circumstances. But we...

3 minutes read.