What does 'self' refer to in a @classmethod?

Yes - that Jake. picture Yes - that Jake. · Feb 10, 2009 · Viewed 13.2k times · Source

I thought I was starting to get a grip on "the Python way" of programming. Methods of a class accept self as the first parameter to refer to the instance of the class whose context the method is being called in. The @classmethod decorator refers to a method whose functionality is associated with the class, but which doesn't reference a specific instance.

So, what does the first parameter of a @classmethod (canonically 'self') refer to if the method is meant to be called without an instance reference?

Answer

SilentGhost picture SilentGhost · Feb 10, 2009

class itself:

A class method receives the class as implicit first argument, just like an instance method receives the instance.

class C:
    @classmethod
    def f(cls):
        print(cls.__name__, type(cls))

>>> C.f()
C <class 'type'>

and it's cls canonically, btw