Python – How Are classmethod and staticmethod Implemented?

built-inclassmethodspython

At what point do methods in Python acquire a get property? —As soon as they're defined in the class? Why does Python let me define a method without any arguments (not even a first self argument)?

I know how to use classmethod and staticmethod, and I know that they're built-in functions, but what happens to a function that is so-decorated?

Essentially, I'm wondering about the "magic" that happens between class definition and class construction.

Best Answer

Check this out.

https://docs.python.org/3/howto/descriptor.html#class-methods

You can also take a look at the source code for class and static method objects, in funcobject.c:

http://hg.python.org/cpython/file/69b416cd1727/Objects/funcobject.c

Class method object definition starts on line 694, while static method object definition starts on line 852. (I do find it kind of funny that they have items titled "method" in funcobject.c when methodobject.c also exists.)