Python Glossary Русский

all Python 3typesterm

types.BuiltinFunctionType

function types

The type of built-in functions and methods written in C: len, print, [].append. They are not FunctionType even though you call them the same way.

Syntax

isinstance(obj, types.BuiltinFunctionType)

A common mistake is to test "is it a function" with isinstance(x, types.FunctionType): for len, print and sorted the answer is False, because they have no Python code and no __code__ attribute. BuiltinFunctionType covers both C module functions (math.sqrt) and methods already bound to an object ([].append); types.BuiltinMethodType is a second name for the same class. To accept any function, use callable() or inspect.isroutine() rather than a list of types.

Examples

import math
import types
for obj in (len, math.sqrt, [].append):
    print(isinstance(obj, types.BuiltinFunctionType),
          isinstance(obj, types.FunctionType))
# → True False / True False / True False
print(types.BuiltinMethodType is types.BuiltinFunctionType)  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy