×

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

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

3 minutes read.

How to Install Python In Windows

How To Install Python In Windows? Python is a language that every aspirant developer looks forward to. One thing we must keep in our mind is how to begin our hands-on...

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.

How to learn python Online

Need to Learn a Programming Language 1. To get a high Paying job We know that Software Engineering is one of the top-paying professions in the world. The top criteria to be...

3 minutes read.

End Parameter in python

print(): The python print() function prints the program’s output to the output screen. The output can be an integer value, string value or other value. Syntax: print(“hi”) Output: hi will be displayed on the output...

3 minutes read.

Python bool()

Python bool() class The bool() class in Python returns the boolean value for the specified object. Syntax class bool([x]) Parameter object: This parameter represents the object, like String, List, Number etc. Return It will always return True, except...

1 minute read.

Python Variable Scope with Local & Non-local Examples

This article will examine Python's global, local and non-local variables and show you how to use them to write code without problems. Let's quickly review what a variable in Python is...

8 minutes read.

Python Project Ideas

One of the most widely used programming languages today is Python. This pattern appears set to continue through 2023 and beyond. Therefore, working on some current Python project ideas is the...

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

Standard GUI Unit Converter using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

12 minutes read.

Python Rest API

In this tutorial, we will understand the meaning of API and REST API. We will understand the working of REST API. We will then realize the boundaries of architecture defined...

4 minutes read.

How to Iterate a List in Python

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

3 minutes read.

Python K-Means Clustering

K-Means Clustering is a basic yet incredible calculation in information scienceThere are a plenty of true utilizations of K-Means clustering (a couple of which we will cover here)This far reaching...

25 minutes read.

API Requests using Python

What is an API? API stands for Application Programming Interface. It is commonly known as API. It provides an environment that helps two or more computer programs to contact each other....

5 minutes read.

Number pattern in Python

Number pattern in Python: This article explains how to print number patterns in Python. The FOR loop, while loop, and range() functions are used in the following Python programs to...

4 minutes read.

Enumerate() Function in Python

The enumerate() function in Python is used to convert a data collection object into an enumerate object. It returns an object that contains a counter as a key for each...

3 minutes read.

How to handle exceptions in python

Prerequisite: Exceptions and errors in Python What are Exceptions? Exceptions are run-time errors that unexpectedly occur and crash a program. When occurred, it alters the flow of the program. These errors are...

9 minutes read.

Python Project Ideas Based On Django

Introduction If you have learned Python and you are an expert in Django, then your practical skills should be excellent in this field. If you want to check your practical skills,...

9 minutes read.

String indices must be integers in Python

Lists, tuples, and strings are examples of iterable objects in Python whose items or characters can be retrieved by their index numbers. For instance, you might take the following action to...

3 minutes read.

How to set font for Text in Python

As a tuple with the font family as the first component, a size in point as the second, and possibly a string with one or more of the style modifiers...

5 minutes read.