Python callable() Function – Tutorial with Examples

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.



object – the object to be tested for callability.

Return value

The callable() function returns True if the object is callable, and False otherwise.


Example 1: Testing a function

def func():
    print("Hello, World!")

Output: True

Example 2: Testing a class

class MyClass:
    def __call__(self):
        print("Hello, World!")
obj = MyClass()

Output: True

Example 3: Testing an integer

n = 10

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.

Leave a Reply

Your email address will not be published. Required fields are marked *