Python callable() Method

The callable() method returns True if the object passed is callable, False if not.

In Python, classes, methods, and instances are callable because calling a class returns a new instance. Instances are callable if their class includes __call__() method.

Syntax:

callable(object)

Parameters:

object: Required. The object to be checked.

Return Value:

Returns True if an object is callable else, returns False.

The following example checks built-in objects and methods to see if they are callable or not.

Example: callable()
print("Is str callable? ", callable(str)) # str class
print("Is len callable? ", callable(len)) # len function
print("Is list callable? ", callable(list)) # list class

num=10
print("Is variable callable? ", callable(num)) 
Output
Is str callable? True
Is len callable? True
Is int callable? True
Is variable callable? False

The callable() method works with user-defined classes and functions, as shown below.

Example: callable()
class student:
    def greet(self):
        print("Hello there")

std = student()
print("Is student class callable? ",callable(student))
print("Is student.greet() callable? ",callable(std.greet))
print("Is student instance callable? ",callable(std))

Output
Is student class callable? True
Is student.greet() callable? True
Is student instance callable? False

In the above example, the std instance is not callable. Calling the object will raise an error. To make the instance callable, you must override the __call__() method in the student class, as shown below.

Example: Make Instance Callable
class student:
    def greet(self):
        print("Hello there")
        
    def __call__(self):
        print("Hello, I am a student.")

std = student()
print("Is student instance callable? ", callable(std))
print(std())
Output
Is student instance callable?  True
Hello, I am a student.
Want to check how much you know Python?