[ACCEPTED]-python 3: how to check if an object is a function?-python-3.x
Accepted answer
in python2:
callable(fn)
in python3:
isinstance(fn, collections.Callable)
as Callable is an 2 Abstract Base Class, this is equivalent 1 to:
hasattr(fn, '__call__')
How can I check that an object is a function?
Isn't 2 this same as checking for callables
hasattr(object, '__call__')
and also 1 in python 2.x
callable(object) == True
You can do:
def is_function(x):
import types
return isinstance(x, types.FunctionType) \
or isinstance(x, types.BuiltinFunctionType)
0
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.