Introducing the "for" loop

Chris Angelico rosuav at gmail.com
Sat Oct 7 14:47:18 EDT 2017


On Sun, Oct 8, 2017 at 3:58 AM, Terry Reedy <tjreedy at udel.edu> wrote:
> No built-in function is an instance of FunctionType
>>>> isinstance(compile, FunctionType)
> False
>>>> isinstance(print, FunctionType)
> False
>>>> type(compile)
> <class 'builtin_function_or_method'>
>>>> type(int.bit_length)
> <class 'method_descriptor'>
>
>
> FunctionType == function defined by def statement or lambda expression.
> These are a subset of functions defined by Python code.
>
> Unless one means 'function defined by def or class', excluding all functions
> defined in the interpreter implementation language, which can change as
> modules are recoded one way or the other, and some functions defined in
> Python, FunctionType is too narrow.  The predecate would have to be at least
>
> BuiltinFunction = type(compile)
> MethonDescriptor = type(int.bit_length)
> isinstance(x, (FunctionType, BuiltinFunction, MethodDescriptor))
>
> but that still excludes bound methods and partial functions.

The three types lack a concrete base class... but fortunately, they
have an abstract one.

>>> isinstance(print, collections.abc.Callable)
True
>>> isinstance(int.bit_length, collections.abc.Callable)
True
>>> isinstance(lambda: 1, collections.abc.Callable)
True
>>> isinstance(range, collections.abc.Callable)
True

Now I know my ABCs. :)

ChrisA



More information about the Python-list mailing list