×

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 math.cos and math.acos function

Math.cos() function In Python, the Math module is used for performing the mathematical operations. It includes the math.cos() function that is used for obtaining the cosine value of an angle in...

3 minutes read.

Python Modulo

For basic calculations, Python provides operators. Python supports a broad range of Arithmetic Operators to do arithmetic, as given below: +Addition*Multiplication-Subtraction/Division//Floor division**Exponentiation%Remainder/Modulus As you can see, one of these basic arithmetic operators...

8 minutes read.

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

3 minutes read.

Python String splitlines() method

Python String splitlines() method The string.splitlines() method in Python splits the specified string and returns a list of the lines in the string, breaking at line boundaries.  Syntax splitlines([keepends]) Parameter keepends(optional): This parameter specifies if...

1 minute read.

Python - Binomial Distribution

Introduction Definition of the Binomial Distribution The method of counting how many instances of a specific event there have been is called the binomial distribution. It will outline the possible outcomes or...

4 minutes read.

Introduction to Scratch programming

Scratch programming is generally designed for children who may create digital stories, games, and animations using the computer language Scratch, which has the largest kid-focused community in the world. Scratch...

3 minutes read.

Python String join() method

Python String join() method The String.join() method in Python concatenates each element of an iterable (such as list, string and tuple) to the given string and returns the concatenated string. Syntax string.join(seq) Parameter Seq: This...

1 minute read.

tell() function in Python

Introduction The tell() function in Python is used to return the current position of the file read/write pointer within the file. An integer value is returned by this method, and it...

2 minutes read.

Python exit commands

exit(), quit(), sys.exit(), os._exit() In this tutorial, we will study exit commands used in the Python programming language. Python is undoubtedly the choice of programmer and this is because of the in-built...

3 minutes read.

Import py file in Python

In Python programming language, a module is a single layer of block of Python code that can be loaded and used by importing into other Python block of code. A module...

4 minutes read.

What Does the Percent Sign (%) Mean in Python?

In python, the percent sign is called modulo operator " %, " which returns the rest of partitioning the left-hand operand by the right-hand operand. Example: value1 = 8 value2 = 2 remainder =...

4 minutes read.

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 MySQL Database Connection

In this article, you will be able to understand how to connect to a MySQL database through Python. We generally can use many methods or modules to connect to the database...

6 minutes read.

Add in Dictionary Python

Dictionary in python: Dictionaries are a useful data configuration for storing information in Python because they can mimic real-world data arrangements where a particular value exists for a particular key. The...

4 minutes read.

Python globals() function

Python globals() function The globals() function in Python returns the value of the named attribute of object where the ‘name’ must be a string. Syntax globals() Parameter NA Return This function returns the global symbol table as a dictionary. Example...

1 minute read.

Python Dictionary pop() method

Python Dictionary pop() method The dictionary.pop() method in removes the specified item and returns an element from a dictionary having the given key. Syntax dictionary.pop(key[, default]) Parameter key – This argument signifies the key which is to...

2 minutes read.

Python Bitwise Operators

When used with variables and objects in an expression, operators are tokens that carry out calculations. The variables and items to which the computation is applied are known as operands....

5 minutes read.

Python EOL (End Of Line)

Introduction An EOL (End Of Line) is defined as a syntax error that indicates that the Python interpreter reached at the end of the line when it tried to scan a...

3 minutes read.

Writing to a CSV file in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

6 minutes read.

How to check data type in python

The data type represents the nature of a variable; it determines what kind of data it can store and what operations can be carried out on it. There are five...

4 minutes read.