Python – Find Object Callable – callable() Function with Examples

callable() function is used to find the object callable in Python, if it appears callable, true will return (remember that it is still possible that a call fails) and false will return if it is not callable (in this case calling the object will never be succeeded).
callable() function is a part of Python Built-in Functions.

 

Syntax of callable() Function

The syntax of callable() function in Python is:

callable(object)

 

Parameters of callable() Function in Python

x – Where object is any object.

 

Compatibility

callable() function is available and compatible with both Python 2.x and 3.x.

 

Return Value of callable() Function in Python

callable() function return boolean values true or false, it return true if the object is callable and return false if the object is not callable. callable() return false if the function fails.

 

Python callable() Function Example 1

a = 33
# callable function will return false
print(callable(a))


def my_test_function():
    print("This is a test")


b = my_test_function
# but here callable function will return true
print(callable(b))

Output:
False
True

 

Python callable() Function Example 2

class MyClass:
  def __call__(self):
    print('Some print text')


print(callable(MyClass))

InstanceOfMyClass = MyClass()
# You cans see the object is callable
InstanceOfMyClass()

Output:
True
Some print text

 

Python callable() Function Example 3

class MySecondClass:
  def printLine(self):
    print('Some Text')

print(callable(MySecondClass))

Output:
True

 

Python callable() Function Example 4

class MyThirdClass:
  def printLine(self):
    print('Some Text')

print(callable(MyThirdClass))

InstanceOfMyThirdClass = MyThirdClass()
# Raises an Error
# 'MyThirdClass' object is not callable
InstanceOfMyThirdClass()

Output:
True
Error: 'MyThirdClass' object is not callable