The callable()
function in Python is a built-in function that is used to check if an object is callable or not. In Python, an object is considered callable if it can be called like a function, i.e., if it has a __call__
method that can be invoked.
Syntax
callable(object)
object
– the object to be tested for callability.
Return value
The callable()
function returns True
if the object is callable, and False
otherwise.
Examples
Example 1: Testing a function
def func(): print("Hello, World!") print(callable(func))
Output: True
Example 2: Testing a class
class MyClass: def __call__(self): print("Hello, World!") obj = MyClass() print(callable(obj))
Output: True
Example 3: Testing an integer
n = 10 print(callable(n))
Output: False
In conclusion, the callable()
function is a useful tool for checking if an object is callable in Python. It returns True
if the object has a __call__
method that can be invoked, and False
otherwise. This function can be used to avoid TypeError exceptions when calling objects that are not callable.