Menu Close

Static Method in Python

In this Python guide, we are going to learn all about Python static method using appropriate examples. static method in Python is a special type of method that does not associate with any object.

Before going through this article, you should have basic knowledge of Python programming.

What is a static method in Python?

The static method in Python is similar to the Python class-level method. In the simple class-level method first argument is always self but in the static method, there is no implicit argument is passed.

static method in Python bound to the class rather than the object of the class. That means we can call the static method without using the object.

Python static method can not be able to modify the state of the object.

You have to remember some points when you are working with static methods in Python.

  • A static method is a method that associates with a class not the object of that class.
  • A static method is not able to change the state of the class.

In Python, there are two ways to create static methods in Python.

  • staticmethod()
  • @staticmethod

staticmethod()

staticmethod() is a built-in Python function that returns a static method for a given function.

Let’s create a static method using staticmethod() built-in function.

Parameters

The staticmethod() method takes a single parameter.

  • function:- function that needs to be converted to a static method

Example


class Calculator:
	def Addition(x, y):
		print(f"Sum of {x} and {y} is :- {x+y}")	
Calculator.Addition = staticmethod(Calculator.Addition)
Calculator.Addition(12, 23)

When you run the above program you will get an output like.

Sum of 12 and 23 is :- 35

@staticmethod Decorator:

You can use the @staticmethod decorator to create a static method. let’s create the above program using the @staticmethod decorator.

Example

class Calculator:
	@staticmethod
	def Addition(x, y):
		print(f"The Sum of {x} and {y} is:- {x+y}")
		
		
Calculator.Addition(12, 23)	

When you run the above program you will get output like this:

The Sum of 12 and 23 is:- 35

Conclusion

In this guide, we have learned all about Python static methods along with examples. The only difference between the static method in Python and the simple class method is that the static method does not take any implicit parameter but the simple class method has self parameter as the first argument.

If you like this article, please share and keep visiting for further Python articles.

For more information:- Click Here

Frequently Asked Questions


What is a static method in Python?

Ans: A static method is just like a method that is bound to the class itself rather than its object.
They do not require instance or object creation of the class to access the static method.

How do you call the static method in Python?

Ans:- Static method in Python can be called without creating the instance or object of the class. Simply create the method and call it directly.

Is there a static method in Python?

Ans: The static method in Python is extremely similar to the Python class-level method, The only difference being that the static method doesn’t take any parameter implicitly.

What is the difference between the Python class method and the static method?

Ans:- A class method can access or modify class objects but a static method can’t access or modify. In simple, static method does not know any about class.

Write CSV Files in Python
Python Function Tutorial

Related Posts