Class vs static methods in Python

In this article I'll try to explain what are staticmethod and classmethod, and what the difference is between them. staticmethod and classmethod both use decorators for defining a method as a staticmethod or classmethod. Please take a look at the article Python Decorators Overview for a basic understanding of how decorators works in Python.

Simple, static and class methods

The most used methods in classes are instance methods, i.e instance is passed as the first argument to the method.

For example, a basic instance method would be as follows:

This gives us the following output:

Python Instance Method Example

After looking at the code sample and diagram:

  • In 1 and 2, the arguments are passed to the method.
  • On 3, the self argument refers to the instance.
  • At 4, we do not need to provide the instance to the method, as it is handled by the interpretor itself.

Now what if the method we want to write interacts with classes only and not instances? We can code a simple function out of the class to do so but that will spread the code related to class, to out of the class. This can cause a future code maintenance problem, as follows:

Gives us the following output:

The Python @classmethod

What we want to do now is create a function in a class, which gets the class object to work on instead of the instance. If we want to get the no of instances, all we have to do is something like below:


Using features introduced after Python 2.2, we can create a method in a class, using @classmethod.

We get the following output:

The benefit of this is: whether we call the method from the instance or the class, it passes the class as first argument.

The Python @staticmethod

Often there is some functionality that relates to the class, but does not need the class or any instance(s) to do some work. Perhaps something like setting environmental variables, changing an attribute in another class, etc. In these situation we can also use a function, however doing so also spreads the interrelated code which can cause maintenance issues later.

This is a sample case:

Which gives us the following output:

Here if we use a @staticmethod, we can place all code in the relevant place.

Which gives us the following output:

Here is a more comprehensive code example, with a diagram to show you

How @staticmethod and @classmethod are different.

Here's a diagram to explain what's going on:

Comparison of @staticmethod vs @classmethod

To Practice: Try this interactive course on the basics of Lists, Functions, Packages and NumPy in Python.

About The Author

  • Tom

    Thanks for this. Makes a lot of sense now. Always been confused by classmethods

  • Rahul

    total crap….

    • 李保银

      Could I translate other article of your site to chinese on my blog? — Baoyin.Li

      • Sure, feel free :). As long as you link back to the original article.

        • 李保银

          OK, I will link to original site always.

  • Pingback: pyhton静态方法和类方法 | 勤思录()

  • yaqian

    There is an error in the diagram. we should put the cross label on Kls.printed, not Kls.smethod.

  • yaqian

    put the cross label on Kls.printed, not ik.smethod.

  • Alexander G. Riccio

    Lines 10-17 in the @staticmethod sample case need indentation.

    I love the post!

    • Alexandre

      thanks for pointing that out! it confused me.

  • Alexandre

    love the fact that you’re using all these examples and diagrams!
    the classmethod example was great – now I know when to use those.
    but I still don’t understand when I would want to use staticmethods, because it seemed to me that the staticmethod examples were useless functions that didn’t really do anything.
    also, what does one call a method that neither a classmethod nor a staticmethod? an instance method?

  • Lorena Mesa

    Love this! THX!

  • Swarvanu Sengupta

    can we call __init__ from static method inside of a class..

  • Ali

    I love this post thanks Arun

  • Annie Chang

    `ik1.do_reset()` and `ik1.set_db()` can’t work in python 2.7.

    Guess this is an article for python 3.0.

  • kamelzcs

    It is really great post!

  • This is fantastic. Really helped me to understand like no other blog article!

  • csukuangfj

    Good examples. Thanks a lot.

  • thanks for the post, Arun

  • Jake Locker

    THANK YOU! I have been trying to find a post which actually describes the difference between static and class methods. Everyone just uses interchangeable code that doesn’t explain it. Thanks for finally pointing it out!

  • ali reza


  • Pingback: All about Python Static and Class methods | My Blog on everything..()

  • Pavlo Kochubei

    Thanks, Arun! Great explanation!

  • Pingback: Difference between @staticmethod and @classmethod in Python – Monologue()

  • Pingback: 编写高效且优雅的 Python 代码(1)-演道网()