[ACCEPTED]-python 3: how to check if an object is a function?-python-3.x

Accepted answer
Score: 24

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__')
Score: 5

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
Score: 3

You can do:

def is_function(x):
    import types
    return isinstance(x, types.FunctionType) \
        or isinstance(x, types.BuiltinFunctionType)

0

More Related questions