Python – What are “class methods” and “instance methods”, in Python

pythonterminology

There has been a discussion in chat relating to a question (the question itself being irrelevant to this one), that has revealed I may not know Python whatsoever.

In my mind, although terminology differs across languages, we may generally categorise functions as:

  • [free] functions
  • static methods / static member functions
  • non-static methods / non-static member functions

Apparently in Python there is another kind of function that doesn't fit into the above categories, one that is a method but "doesn't know its class".

What are "class methods" and "instance methods", in Python?

Best Answer

The short answer

  • an instance method knows its instance (and from that, its class)
  • a class method knows its class
  • a static method doesn't know its class or instance

The long answer

Class methods

A class method is one that belongs to the class as a whole. It doesn't require an instance. Instead, the class will automatically be sent as the first argument. A class method is declared with the @classmethod decorator.

For example:

class Foo(object):
    @classmethod
    def hello(cls):
        print("hello from %s" % cls.__name__)
Foo.hello()
-> "Hello from Foo"
Foo().hello()
-> "Hello from Foo"

Instance Methods

On the other hand, an instance method requires an instance in order to call it, and requires no decorator. This is by far the most common type of method.

class Foo(object):
    def hello(self):
        print("hello from %s" % self.__class__.__name__)
Foo.hello()
-> TypeError: hello() missing 1 required positional argument: 'self'

(note: the above is with python3; with python2 you'll get a slightly different error)

Static methods

A static method is similar to a class method, but won't get the class object as an automatic parameter. It is created by using the @staticmethod decorator.

class Foo(object):
    @staticmethod
    def hello(cls):
        print("hello from %s" % cls.__name__)

Foo.hello()
-> TypeError: hello() missing 1 required positional argument: 'cls'

Documentation links

Here are links to the relevant python3 documentaton:

The data model documentation has this to say about the difference between class methods and static methods:

Static method objects Static method objects provide a way of defeating the transformation of function objects to method objects described above. A static method object is a wrapper around any other object, usually a user-defined method object. When a static method object is retrieved from a class or a class instance, the object actually returned is the wrapped object, which is not subject to any further transformation. Static method objects are not themselves callable, although the objects they wrap usually are. Static method objects are created by the built-in staticmethod() constructor.

Class method objects A class method object, like a static method object, is a wrapper around another object that alters the way in which that object is retrieved from classes and class instances. The behaviour of class method objects upon such retrieval is described above, under “User-defined methods”. Class method objects are created by the built-in classmethod() constructor.

Related questions