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 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 is self but in the static method, there is not any implicit argument is passes.

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 method in Python.

  • A static method is a method that associates with 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 method in Python.

  • staticmethod()
  • @staticmethod

staticmethod()

staticmethod() is a built-in python function which return static method
for given function.

Let’s create 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 above program you will get 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 above program you will get output like:

The Sum of 12 and 23 is:- 35

Conclusion

In this guide, we have learned all about Python static method 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:- Static method is just like a method that 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. Simple create the method and call it directly.

Is there a static method in Python?

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

Python Abstract Class
Instance method in Python

Was this post helpful?

Related Posts