Open In App

callable() in Python

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to check if an object is callable in Python. In general, a callable is something that can be called. This built-in method in Python checks and returns True if the object passed appears to be callable, but may not be, otherwise False.

Python callable()

In Python, callable() function is a built-in function that we can use to check if an object is callable in Python i.e., it can be called like a function. It generally returns True if the object can be called and False if not.

callable() in Python Syntax

Syntax: callable(object)

The callable() method takes only one argument, an object, and returns one of the two values

  • returns True, if the object appears to be callable.
  • returns False, if the object is not callable.

How callable() work in Python?

In this example, we have used callable to find out if the object is callable or not.

Python3




x = 10
 
print(callable(x))
 
def geeks(x):
    return (x)
 
y = geeks
print(callable(y))


Different Scenario for Python callable()

There may be few cases where callable() works differently and returns True but the call to object fails. But if a case returns False, the calling object will never succeed.

When Object is Callable

In this example, we are using callable() function to check if an object is callable in Python. In the first case when an object is passed in the callable() method, it returns True. It is so because let is an object to the callable function Geek (which may not be in all cases). In the second case num is absolutely not a callable object, so the result is False.

Python3




# Python program to illustrate
# callable() a test function
def Geek():
    return 5
 
# an object is created of Geek()
let = Geek
print(callable(let))
 
# a test variable
num = 5 * 5
print(callable(num))


Output

True
False


In this example, we have made a class Geek to check if the class and object is callable or not.

Python3




# Python program to illustrate callable()
class Geek:
    def __call__(self):
        print('Hello GeeksforGeeks')
 
# Suggests that the Geek class is callable
print(callable(Geek))
 
# This proves that class is callable
GeekObject = Geek()
GeekObject()


Output

True
Hello GeeksforGeeks


When Object is NOT callable

In this example, we will using callable() function to check if the object is callable or not in Python. Here the object is not callable. The callable() method returns True suggesting that the Geek class is callable, but the instance of Geek is not callable() and it returns a runtime error.

Python3




# Python program to illustrate callable()
class Geek:
  def testFunc(self):
    print('Hello GeeksforGeeks')
 
# Suggests that the Geek class is callable
print(callable(Geek))
 
GeekObject = Geek()
# The object will be created but
# returns an error on calling
GeekObject()


Output:

True
Traceback (most recent call last):
File "/home/3979dc83032f2d29befe45b6ee6001a4.py", line 10, in
GeekObject()
TypeError: 'Geek' object is not callable



Last Updated : 29 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads