# @classmethod vs @staticmethod vs "plain" methods # What's the difference? class MyClass: def method(self): """ Instance methods need a class instance and can access the instance through `self`. """ return 'instance method called', self @classmethod def classmethod(cls): """ Class methods don't need a class instance. They can't access the instance (self) but they have access to the class itself via `cls`. """ return 'class method called', cls @staticmethod def staticmethod(): """ Static methods don't have access to `cls` or `self`. They work like regular functions but belong to the class's namespace. """ return 'static method called' # All methods types can be # called on a class instance: >>> obj = MyClass() >>> obj.method() ('instance method called', <MyClass instance at 0x1019381b8>) >>> obj.classmethod() ('class method called', <class MyClass at 0x101a2f4c8>) >>> obj.staticmethod() 'static method called' # Calling instance methods fails # if we only have the class object: >>> MyClass.classmethod() ('class method called', <class MyClass at 0x101a2f4c8>) >>> MyClass.staticmethod() 'static method called' >>> MyClass.method() TypeError: "unbound method method() must be called with MyClass " "instance as first argument (got nothing instead)"
self is used in instance methods,
cls is often used in class methods.
class Foo(object): # you couldn't use self. or cls. out here, they wouldn't mean anything # this is a class attribute thing = 'athing' def __init__(self, bar): # I want other methods called on this instance of Foo # to have access to bar, so I create an attribute of self # pointing to it self.bar = bar @staticmethod def default_foo(): # static methods are often used as alternate constructors, # since they don't need access to any part of the class # if the method doesn't have anything at all to do with the class # just use a module level function return Foo('baz') @classmethod def two_things(cls): # can access class attributes, like thing # but not instance attributes, like bar print cls.thing, cls.thing