The pythonic way to check the type of a function is using isinstance
builtin.
i = 2
type(i) is int #not recommended
isinstance(i, int) #recommended
Python includes a types
module for checking functions among other things.
It also defines names for some object types that are used by the
standard Python interpreter, but not exposed as builtins like int or
str are.
So, to check if an object is a function, you can use the types module as follows
def f():
print("test")
import types
type(f) is types.FunctionType #Not recommended but it does work
isinstance(f, types.FunctionType) #recommended.
However, note that it will print false for builtin functions. If you wish to include those as well, then check as follows
isinstance(f, (types.FunctionType, types.BuiltinFunctionType))
However, use the above if you only want specifically functions. Lastly, if you only care about checking if it is one of function,callable or method, then just check if it behaves like a callable.
callable(f)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…