×

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 Virtual Environment

In this tutorial, we will understand Virtual Environment in Python. We will understand the need for a virtual environment and also see how to make use of it in python....

3 minutes read.

Python Command line Arguments

Python Command line Arguments The command-line argument is used to the change functionality of the program. It's an extra command the programmer can use while launching a program. These commands have many uses...

7 minutes read.

Convert int to Float in Python using Pandas

Introduction We have already learnt about how to convert float variables to int. Now let us know how to convert int variables to float So let us create another Dataset on which...

2 minutes read.

Gaussian elimination in python

Linear and polynomial equations are used in almost all fields of numerical simulation. However, its most common use in engineering is in the area of linear system analysis. The broader...

3 minutes read.

N2 in Python

N2 is known as Nearest Neighbor Algorithm, because it contains 2 N's (N-Nearest, N-Neighbor). This is a library in the python build using C++ and Python. Before N2 was made,...

3 minutes read.

Closest Pair of Points in Python

We are given an array of n points in the plane, and our task is to find the pair of points in the array that are the closest to each...

3 minutes read.

Python Classification

Identification and grouping of items or concepts into specified categories this process is known the classification. Data can be separated and sorted in data management according to predetermined criteria for...

6 minutes read.

How to Read html page in python

Html is a Hyper Text Markup language is a standard language used for creating webpages. HTML is the name of the language used to describe the construction of Web pages....

4 minutes read.

Loan Calculator using PyQt5 in Python

In the following tutorial, we will learn how to build a Loan Calculator application using the PyQt5 library in the Python programming language. So, let's get started. Introduction to the code: The heading...

4 minutes read.

Data Structures and Algorithms Using Python | Part 1

Data Structures: Data Structure is defined as a way to organize and store the data so that we can access the data and work more efficiently. Data structures also describe the...

18 minutes read.

List Assignment Index out of Range in Python

As we know, a List is one of the four unique data structures available in Python; In this tutorial, we will deep dive into understanding iterating through a list and...

3 minutes read.

any() Keyword in python

“any” keyword in python This page contains all the information about any () Keyword in python, how it is used with lists, tuples, dictionaries, sets, and what its function is? Python...

3 minutes read.

Python Program to find the length of a string

Python program to find the length of a string A string in Python is a set of Unicode characters. It can't be changed once it's been declared. The number of characters...

2 minutes read.

Cross Validation in Sklearn

Data scientists can benefit from cross-validation in machine learning in two key ways: it can assist in minimising the amount of data needed and ensure the artificial intelligence model is...

14 minutes read.

Python Slice from Last Occurrence of K

Introduction We already know that in Python, cutting produces a sub-string out of a string. The variables start, stop, and step are used to set the slicing range. When dealing on...

3 minutes read.

Spyder (32-bit) - Free download

Spyder is a free and open-source scientific environment created by and for Python engineers, scientists, and data analysts. It is a robust scientific environment created in Python by and for...

3 minutes read.

Introducing modern python computing in simple packages

Python is the language for newly evolving technologies and has become one of the most popular computer languages in the world. Python is used in everything right from a simple...

3 minutes read.

Arguments and parameters in Python

Generally, there is a misunderstanding that parameters and arguments, both are the same. But, as a programmer, you need to understand the difference between these two. Parameters: While defining a function, we...

5 minutes read.

Amazon rekognition using python

Amazon recognition service is an AI service which is an image labelling API (Application programming interface). In this article, we shall learn to use the AWS python boto3 module to...

3 minutes read.

Compound Interest GUI Calculator using PyQt5 in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

6 minutes read.