Where is the source code for the decorator classmethod located in the python source code. Specifically I am having trouble finding the exact file it’s defined in version 2.7.2
Where is the source code for the decorator classmethod located in the python source
Share
I am not answering what you asked – but the code below shows what could be a decorator equivalent to
classmethod, written in Pure Python – since the one in the source code is in C, inside Python source code as Mishna puts in his answer (link updated for cPython development branch on GitHub).So, the idea of class methods is to use the "descriptor" mechanism, as described in Python’s data model – and make it so that the
__get__method does return a function object that when called, will call the original method with the first argument pre-filled:And on Python console:
*** EDIT – Update ***
The O.P. further asked "If I wanted the decorator to also accept a parameter what would be the proper format for init? " –
In that case it is not only
__init__which has to be changed – a decorator that accepts configuration parameters is actually called in "two stages" – the first one annotate the parameters, and returns a callable – the second call accepts only the function which will actually be decorated.There are a few ways to do it – but I think the most straightforward is to have a function that returns the class above, like in: