I am not responding to what you requested, but the following shows what could be a decorator equivalent to the classmethod written in Pure Python, since the one that is in the source code is inside C, inside Python-2.7.2/Objects/funcobject.c , as Mishnah answers.
So, the idea of class methods is to use the “descriptor” mechanism, as described in the Python data model — and make the __get__ method return a function object that, when called, will call the original method with the first argument pre-populated:
class myclassmethod(object): def __init__(self, method): self.method = method def __get__(self, instance, cls): return lambda *args, **kw: self.method(cls, *args, **kw)
And in the Python console:
>>> class MyClass(object): ... @myclassmethod ... def method(cls): ... print cls ... >>> >>> m = MyClass() >>> m.method() <class '__main__.MyClass'> >>>
* EDIT - update *
The OP further asked, “If I wanted the decorator to also accept a parameter, which would be the correct format for init?” -
In this case, it is necessary to change not only __init__ - the decorator that accepts the configuration parameters is actually called in "two stages" - the first annotates the parameters and returns the called - the second call is accepted only by the function that will actually be decorated.
There are several ways to do this, but I believe that the simplest task is a function that returns the class above, for example:
def myclasmethod(par1, par2, ...): class _myclassmethod(object): def __init__(self, method): self.method = method def __get__(self, instance, cls):