[Python-ideas] Add the method decorator

Serhiy Storchaka storchaka at gmail.com
Thu Apr 12 09:57:01 EDT 2018


There is a difference between functions implemented in Python and C. 
Functions implemented in Python are descriptors. They can be used for 
defining methods in Python classes. Functions implemented in C are not 
descriptors. When set a class attribute to a functions implemented in C, 
it will not become a bound method.

     from _noddy import noddy_name
     class Noddy:
         name = noddy_name
     noddy = Noddy()

If noddy_name is a Python function, noddy.name() will call 
noddy_name(noddy), but if it is a C function, noddy.name() will call 
noddy_name().

The same is true for classes and custom callables.

If a function is a descriptor, it can be converted into non-descriptor 
function by wrapping it with the staticmethod decorator. I suggest to 
add the method decorator, which converts an rbitrary callable into a 
descriptor.

     class Noddy:
         name = method(noddy_name)

This will help to implement only performance critical method of a class 
in C. Currently you need to implement a base class in C, and inherit 
Python class from C class. But this doesn't work when the class should 
be inherited from other C class, or when an existing class should be 
patched like in total_ordering.

This will help also to use custom callables as methods.



More information about the Python-ideas mailing list