Author terry.reedy
Recipients jdemeyer, r.david.murray, scoder, serhiy.storchaka, steven.daprano, terry.reedy
Date 2017-04-15.06:06:46
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
inspect.isfunction(object) is documented to
    Return true if the object is a Python function, which includes functions created by a lambda expression.

This is currently implemented as "isinstance(object, types.FunctionType)".

The docs usually regard a 'Python function' as the result of a def statement or lambda expression.  The inspect doc says that a function includes a particular set of attributes.  One of them is a code object with its own fairly extensive set of attributes.  Some of them are derived from the Python code.  But others, in particular co_code, are specific to the current CPython bytecode version.  (And co_code is intentionally writable.)

To me, the main purpose of checking that something is a function, as opposed to just being callable, is to know whether one can dependably access the attributes.  Given that some are inherently CPython specific, including objects compiled by third-party software seems dubious.  (There is also the issue of not being able to test with 3rd party objects.)

The referenced cython doc says
"""While it is quite possible to emulate the interface of functions in Cython’s own function type, and recent Cython releases have seen several improvements here,"""

To me, this implies to me that Cython function (compiled from Cython's extended def statements) do not yet perfectly emulate (fulfill) 'Python functions'.  As indicated above, perfect emulation seems impossible for Cython or any other external compiler that does not use the same bytecode.

"""the “inspect” module does not consider a Cython implemented function a “function”, because it tests the object type explicitly instead of comparing an abstract interface or an abstract base class. This has a negative impact on code that uses inspect to inspect function objects, but would require a change to Python itself."""

Where the current situation would be annoying is if working code uses isfunction and then Cython is used to speed up the code.  But Cython could supply, if it does not now, expanded functions along with the list of cyfunction attributes and an indication of which are compatible with CPython function attributes.

Cython is not the only 3rd party compiler, and not the only one that might ever be linkable to CPython.  So any change to CPython should not be limited to Cython.

If it were possible for Cython to makes its CythonFunction class a subclass of FunctionType, the issue would be 'solved', though the incompatibilities would remain.
Date User Action Args
2017-04-15 06:06:47terry.reedysetrecipients: + terry.reedy, scoder, steven.daprano, r.david.murray, serhiy.storchaka, jdemeyer
2017-04-15 06:06:47terry.reedysetmessageid: <>
2017-04-15 06:06:47terry.reedylinkissue30071 messages
2017-04-15 06:06:46terry.reedycreate