How to Use the Python callable() Method

The Callable() Method In Python

Introduction

In this tutorial, we are going to discuss the Python callable() method along with its uses and working.

Basically, an object or instance is termed callable when it has a defined __call__() function. In that case, we can refer to a.__call__(arg1, arg2, …) in a simpler way, a(arg1,arg2, …). Hence, it becomes callable.

The Python callable() Method

Further, the callable() method in Python makes it easier for the users to identify callable as well as non-callable objects and functions. It is a single argument function, that returns true if the passed object is callable and false if it is not.

The syntax for the method is given below,

callable(obj)

Here, the obj is the instance or object that the user wants to check is callable or not.

Working of the Python callable() Method

Let us look at some examples to have a clear understanding of the callable() method in Python.

When Python callable() returns True

As stated earlier, the method returns true when the passed object is callable. Let us see for what conditions it does so.

#true
def demo():
    print("demo() called!")

#object created
demo_obj = demo

class demo_class:
    def __call__(self, *args, **kwargs): #__call__() is defined here
        print("__call__() defined!")


demo_class_obj = demo_class()

print("demo_obj is callable? ",callable(demo_obj))
print("demo_class is callable? ",callable(demo_class)) #classes are always callable
print("demo_class_obj is callable? ",callable(demo_class_obj))

demo_obj() #calling demo()'s object
demo_class_obj() #calling the demo_class object

Output:

demo_obj is callable?  True
demo_class is callable?  True
demo_class_obj is callable?  True
demo() called!
__call__() defined!

Here,

  • We define demo() function, create its new instance demo_obj,
  • Then define a new class demo_class with __call__() function,
  • And create an object of the class demo_class named demo_class_obj,
  • Finally, check whether the created objects and the class are callable or not. Which we can see from the output, are callable.
  • And at last, we call both the function demo() and the demo_class_obj(). In the case of the demo class’s object call, the __call__() method is executed as we can see from the output.

Note: All classes are callable, so for any class, callable() method returns true. This is evident from the above example, where we try checking the callable() output for demo_class.

When Python callable() returns False

Again, callable() returns false when the passed object is not callable. Let us see for what conditions it does that.

n = 10

class demo_class:
    def print_demo(self):
        print("demo")

demo_class_obj = demo_class()

print("n is callable? ",callable(n))

print("demo_class_obj is callable? ",callable(demo_class_obj))

Output:

n is callable?  False
demo_class_obj is callable?  False

In the code above,

  • We initialize an integer n as n = 10,
  • And then define a class demo_class with member function print_demo(),
  • After that, we create an object of the demo_class named demo_class_obj,
  • And finally, check whether n and demo_class_obj are callable or not, Which from the output above, we can see that are not callable.

n is an integer and clearly cannot be called. Whereas in the case of the demo_class_obj, the class(demo_class) doesn’t have a well defined
__call__() method. Hence is not callable.

Conclusion

So, in this tutorial, we learned about the Python callable() method, and its working. The method is widely used for error proofing a program.

Checking if an object or function is callable or not before actually calling it helps avoid TypeError.

I hope you have a clear understanding of the topic. For any questions feel free to comment below.

References