In Python, callable()
is a built-in function used to check whether a given object is callable. It returns True
if the object is callable, and False
otherwise.
Callable objects include functions, methods, classes, and certain class instances. An object is also considered callable if it defines the __call__()
method.
Here are examples of using callable()
:
def foo():
pass
class bar:
def __init__(self):
pass
def __call__(self):
return "hello"
b = bar()
print(callable(foo)) # True
print(callable(bar)) # True
print(callable(b)) # True
print(callable(1)) # False
print(callable('abc')) # False
In the above example, the function foo
, class bar
, and instance b
are all callable, so callable()
returns True
. The integer 1
and string 'abc'
are not callable, so callable()
returns False
.