Static methods and class methods in Python

In this post, I want to talk about static methods and class methods in Python 2. There are many related questions on Stack Overflow, and I want to summarize the ideas of the answers.

Bound

First we define a class and its methods.

1
2
3
4
5
>>> class MethodsExample(object):
... def __init__(self, var):
... self.var = var
... def foo(self):
... return self.var

If we call the method foo() like showed below, we will have an error. This error basically tells us two things:

  1. the method foo() is an unbound method
  2. it must be called with an instance of the class
    To explain it further, foo() is an unbound method, it should be bound with an instance of the class and this instance should be passed as the first argument of this method.

    1
    2
    3
    4
    >>> MethodsExample.foo()
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: unbound method foo() must be called with MethodsExample instance as first argument (got nothing instead)

    So let’s pass an instance!

    1
    2
    >>> MethodsExample.foo(MethodsExample(233))
    233

    This time it works.

    In fact we don’t need to explicitly pass in an instance.

    1
    2
    3
    4
    >>> MethodsExample(233).foo()
    233
    >>> MethodsExample(233).foo
    <bound method MethodsExample.foo of <__main__.MethodsExample object at 0x7f14261e00d0>>

    Python automatically binds all the methods from the class MethodsExample to any instance (e.g. MethodsExample(233)) of this class. So now foo() is a bound method.

Static methods

By adding a @staticmethod decorator to a method, we can turn the method into a static method.

1
2
3
4
5
6
7
8
9
10
11
>>> class MethodsExample(object):
... def __init__(self, var):
... self.var = var
... @staticmethod
... def static_foo(x):
... return x
...
>>> MethodsExample.static_foo(233)
233
>>> MethodsExample(666).static_foo(233)
233

Like shown above, a static method belongs to a class but the method doesn’t need to be bound to the object at all. It behaves like a plain function except that you can call it from an instance or the class.

So why static methods?

  1. Staticmethods are used to group functions which have some logical connection with a class to the class. If we have a module with several classes in it and foo() is only associated with one of them, then we can write foo() as a static method in that class.
  2. We can override the static method in a subclass

Class methods

1
2
3
4
5
6
7
8
9
10
>>> class MethodsExample(object):
... var = 233
... @classmethod
... def class_foo(cls):
... return cls.var
...
>>> MethodsExample.class_foo()
233
>>> MethodsExample().class_foo()
233

Like static methods, class methods are not bound to an object. But they are bound to a class. Whatever the way you use to access this method, it will always be bound to the class it is attached to, and its first argument will be the class itself. Class methods can also be overriden in a subclass. Class methods’ definition follows Sub class, not Parent class, via inheritance.

People say…

Some people say static methods are useless (or have limited use). Sometimes it’s even better to put them outside the class. And Guido van Rossum said:

We all know how limited static methods are. (They’re basically an accident — back in the Python 2.2 days when I was inventing new-style classes and descriptors, I meant to implement class methods but at first I didn’t understand them and accidentally implemented static methods first. Then it was too late to remove them and only provide class methods.