Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out if an Python object is callable

Tags:

python-3.x

I want to find out if an object is callable or not.

I know that type() would return <class 'method'>. But I don't know how I can check for that (e.g. with isinstance()).

like image 518
buhtz Avatar asked Oct 08 '15 00:10

buhtz


Video Answer


1 Answers

Use the builtin callable():

>>> callable(open)
True
>>> print(callable.__doc__)
callable(object) -> bool

Return whether the object is callable (i.e., some kind of function).
Note that classes are callable, as are instances with a __call__() method.

It's a great investment making an effort to master at least 90% of the builtin functions listed here: https://docs.python.org/3/library/functions.html

like image 192
Elias Dorneles Avatar answered Oct 18 '22 22:10

Elias Dorneles