Glossary

Method

A method is a function defined inside a class body. In Python, there are three kinds: instance methods (the default) take self as the first parameter and operate on the instance; class methods (decorated with @classmethod) take cls as the first parameter and operate on the class itself; and static methods (decorated with @staticmethod) take no implicit first parameter and are essentially regular functions namespaced inside the class.

Instance methods are by far the most common. When you call obj.method(arg), Python transforms this into ClassName.method(obj, arg), automatically passing the instance as self. This is possible because Python implements methods using descriptors — objects that define __get__ — which bind the function to the instance at attribute lookup time.

Class methods are commonly used as alternative constructors — for example, datetime.fromtimestamp() and dict.fromkeys(). Static methods are useful for utility functions that logically belong to the class but don't need access to instance or class state. In modern Python, @staticmethod is used less frequently because a module-level function often serves the same purpose with less ceremony.

Related terms: Class, Self, Dunder Method, Property

Discussed in:

This site is currently in Beta. Please email Chris Paton (cpaton@gmail.com) with any suggestions, questions or comments.