×

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 Syntax Error Invalid Syntax

Python is renowned for its simple and direct syntax. However, we might come across some things that Python doesn't allow if we are learning Python for the very first time or if...

14 minutes read.

Python String center() method

Python String center() method The center() method will center align the string, using a specified character (space is default) as the fill character. Syntax string.center(width[, fillchar]) Parameter width This parameter represents the length of the returned string. fillchar...

1 minute read.

Anaconda python 3.7 download for windows 10 64-bit

Before installing Anaconda, you need to first know what Anaconda actually is. Anaconda is an IDE (Integrated development environment) platform used to code and executes python programs. Anaconda works both online...

3 minutes read.

How to convert Float to Int in Python?

A float value can be converted to an int via type conversion, an explicit method of transforming an operand to a certain type. But it must be noted that such...

3 minutes read.

Append key Value to Dictionary in Python

The Python dictionary is one of the built-in data types. Elements of dictionaries are key-value pairs. In Python, there are numerous ways to add dictionaries. Let's examine some of the...

9 minutes read.

Dynamic Typing in Python

Many key factors for developing a great programming language include how it manages its memory space. This memory space largely depends on empty memory boxes called variables that one creates...

3 minutes read.

Inheritance in Python

Inheritance in Python Object-Oriented Programming provides reusable patterns to the code for restricting the redundancy in development projects. One of the basic principles of Object-Oriented Programming that helps achieve recyclable code...

8 minutes read.

How to reverse a string in python

How to reverse a string in python A Brief About Strings- The String is a data type in Python that has a sequence of characters. This series of characters are represented in...

4 minutes read.

Python Functionalities of Pillow Module

This instructional exercise is about "Pillow" library, one of the significant libraries of python for picture control. Pillow library of python, is an easy to use and also open source...

14 minutes read.

Checking whether a String Contains a Set of Characters in python

In this tutorial, we will learn how to examine or check whether a string contains any set of characters or a substring and if it contains, we will learn how...

6 minutes read.

Python Escape Characters

In this tutorial, we will learn how to use the Escape Characters in Python. Escape Character: Escape Characters are used for some special meaning in our statements. It is denoted or represented...

3 minutes read.

Python Logistic Regression with Sklearn & Scikit

In this article, we'll walk through a tutorial for utilising the Python Sklearn (formerly known as Scikit Learn) package to implement logistic regression. To assist you in remembering the concept,...

18 minutes read.

How to Compare two Lists in Python?

How to Compare two Lists in Python The list is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets [...

4 minutes read.

Python Skyline

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...

3 minutes read.

Kite Python

Kite in Python: The Kite is a package provided by the python programming language; it works with the help of artificial intelligence and helps us write code inside the visual studio....

3 minutes read.

Python Set intersection_update() method

Python Set intersection_update() method The set.intersection_update() method in Python removes the items that is not present in both sets. It is different from the set.intersection() method, because the intersection()method returns a new set, with only  the common elements...

2 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.

Idle python download for Windows

Here, we will be discussing how to get the answer to all questions related to installing Python on Windows. What is IDLE? Integrated Development and Learning Environment, or IDLE, is a shorthand....

3 minutes read.

Slicing of a String in Python

In this tutorial, we will learn about slicing of a string in Python. First of all, let us know what String and Slicing is. String String is a group of characters that...

6 minutes read.

Python String startswith() method

Python String startswith() method The string.startswith() method in Python returns a boolean value ‘True’ if the given string starts with the prefix, otherwise it returns False. Syntax startswith(prefix[, start[, end]]) Parameter prefix: This parameter signifies the value to check. start(optional):...

1 minute read.