Python static method

Filed Under: Python

Python static method

In this quick post, we will learn how to create and use a Python static method. We will also have a look at what advantages and disadvantages static methods offer as compared to the instance methods. Let’s get started.

python static method

What is a static method?

Static methods in Python are extremely similar to python class level methods, the difference being that a static method is bound to a class rather than the objects for that class.

This means that a static method can be called without an object for that class. This also means that static methods cannot modify the state of an object as they are not bound to it. Let’s see how we can create static methods in Python.

Creating python static methods

Python Static methods can be created in two ways. Let’s see each of the ways here:

Using staticmethod()

Let’s directly jump to sample code snippet on how to use the staticmethod() approach:

class Calculator:

    def addNumbers(x, y):
        return x + y

# create addNumbers static method
Calculator.addNumbers = staticmethod(Calculator.addNumbers)

print('Product:', Calculator.addNumbers(15, 110))

Note that we called the addNumbers we created without an object. When we run this program, here is the output we will get:
python staticmethod function
There were no surprises there. This approach is controlled as at each place, it is possible to create a static method out of a class method as well. Let’s see another approach with the same example here.

Using @staticmethod

This is a more subtle way of creating a Static method as we do not have to rely on a statement definition of a method being a class method and making it static at each place you make it static.

Let’s use this annotation in a code snippet:

class Calculator:

    # create addNumbers static method
    def addNumbers(x, y):
        return x + y

print('Product:', Calculator.addNumbers(15, 110))

When we run this program, here is the output we will get:
python static method annotation
This was actually a much better way to create a static method as the intention of keeping the method static is clear as soon as we create it and mark it with the @staticmethod annotation.

Advantages of Python static method

Static methods have a very clear use-case. When we need some functionality not w.r.t an Object but w.r.t the complete class, we make a method static. This is pretty much advantageous when we need to create Utility methods as they aren’t tied to an object lifecycle usually.

Finally, note that in a static method, we don’t need the self to be passed as the first argument.

API Reference: Python Documentation


  1. nag says:

    @staticmethod line also the program is same output

  2. himanshu says:

    good explanation

  3. WA says:

    Good explanation! But the method is called multiplyNums, but, actually, it is summing the numbers?!

    1. Pankaj says:

      Ouch, that’s some really bad naming. Sorry about that, but I hope you learned about the static keyword. I am changing the name to addNums.

      1. Philipp says:

        One more mistake the result off addNumbers is a summation not a product.

  4. Shweta says:

    Thanks ..! Wonderful explanation.

  5. Mike says:

    What does w.r.t mean?

    1. LL says:

      It means “with respect to”.

  6. A Discord User#4063 says:

    Why are you adding the numbers in the first example?

Leave a Reply

Your email address will not be published. Required fields are marked *

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages