The callable()
function in Python is a built-in function that returns True
if the specified object appears callable (i.e., can be called as a function), and False
otherwise. An object is considered callable if it has a __call__()
method or is a function or a class.
Parameter Values
Parameter | Description |
---|---|
object | An object that can be called |
Return Values
callable()
returns a bool
, indicating if an object is callable or not.
How to Use callable()
in Python
The callable()
function in Python is used to determine whether an object is callable (a function) or not. It returns True if the object appears callable; otherwise, it returns False.
def greet():
print('Hello, World!')
callable(greet) # Output: True
In the example below, a class method is defined and then checked using the callable()
function. The output will be True because class methods are callable in Python.
class MyClass:
def display(self):
print('This is a class method')
obj = MyClass()
callable(obj.display) # Output: True
The callable()
function will return False when checking a non-callable object, such as an integer. This is because integer objects are not callable in Python.
num = 10
callable(num) # Output: False