[issue47214] builtin_function_or_method is also either a function or a method

Steven D'Aprano report at bugs.python.org
Mon Apr 4 13:33:18 EDT 2022


Steven D'Aprano <steve at pearwood.info> added the comment:

Perhaps what you want is inspect.isroutine ?

https://docs.python.org/3/library/inspect.html#inspect.isroutine

I agree with Dennis that the isfunction test is for **Python** (def or lambda) functions, not builtins. The docstring for the inspect.is* methods make promises about what attributes an object will have:

def isbuiltin(object):
    """Return true if the object is a built-in function or method.
    Built-in functions and methods provide these attributes:
        __doc__         documentation string
        __name__        original name of this function or method
        __self__        instance to which a method is bound, or None"""


def isfunction(object):
    """Return true if the object is a user-defined function.
    Function objects provide these attributes:
        __doc__         documentation string
        __name__        name with which this function was defined
        __code__        code object containing compiled function bytecode
        __defaults__    tuple of any default values for arguments
        __globals__     global namespace in which this function was defined
        __annotations__ dict of parameter annotations
        __kwdefaults__  dict of keyword only parameters with defaults"""


def (and lambda) functions have a different API from builtin_function_or_method objects. They should be kept separate.

----------
nosy: +steven.daprano

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue47214>
_______________________________________


More information about the Python-bugs-list mailing list