Methods and Functions

Python callable()

Before understanding what the Python callable() function does, let us first try to understand what the call operator is, and what is its significance in the Python programming language.

Call Operator

During the program, when we want to use or invoke a function, we often refer to it as being called. The “calling” is accomplished by following the function name with a pair of parentheses, which some people refer to as the call operator.

Example –

                    

# function declaration
def func():
     print('Have a good day!')

# without call operator/ parentheses
print(func)

# with call operator/ parentheses
func()

Output –

                    

<function func at 0x7f6170efddc0>

Have a good day!

Without using the parentheses, the Python interpreter only prompts the object name. While using the call operator i.e., parentheses, with the function name, it prints the string inside the function body.

But how can we identify if objects in Python are callable without using the parentheses? Calling something which is not callable can be risky.

Example –

Output –

                    

Traceback (most recent call last):
File "", line 2, in 
TypeError: 'str' object is not callable

To address this issue, Python provides a built-in function to check if the object is callable or not. This built-in function is called as Python callable() function.

Definition

  • The Python callable() function returns TRUE if the object passed is callable else returns FALSE if it is not.

Python callable()

Python callable() is a built-in function which states if the object passed to the callable() function is callable or not. To be callable, the object must have a call method. For instance, if we simply define a variable with a value, it is not callable; however, if we create a function, it becomes callable.

  • Syntax –
                    

callable(objects)

  • callable() Parameter

Python callable() accepts only a single parameter.

  • Return value of callable()

The callable() function returns either of the two values:

  1. TRUE – if the object method is callable
  2. FALSE – if the object method is not callable

Note – Even if the callable() function returns TRUE, it is not necessary that the object will always be callable, it may fail too. However, if the function returns FALSE, then the object will be definitely not callable.

How does callable() work?

Example 1 –

                    

# Python program to demonstrate callable()
num = 35
print('Is variable callable? - ', callable(num))

def Function():
     print('Hello')
print('Is function callable? - ', callable(Function))

Output –

                    

Is variable callable? -  False
Is function callable? -  True

Example 2 –

                    

# Python program to demonstrate callable()
txt = 'Hello'
print('Is variable txt callable? - ', callable(txt))

print('Typeof txt - ', type(txt))
print('Is type() callable? - ', callable(type(txt)))

Output –

                    

Is variable txt callable? -  False
Typeof txt -  <class 'str'>
Is type() callable? -  True

Explanation –

In Python, the ‘txt’ variable object is not callable. Hence, our first callable() print statement returns a FALSE value. For the third print statement, we have used the type() function on txt variable. Type() is a built-in function in Python. And since this type() function is been passed into the callable(), the returned output is TRUE, because callable() function is checking the function object ‘type()’.

Callable’s in Python

From the above examples, we can conclude few things; There are few objects which are callable and a few which are not callable. The ones which are callable are –

  • Functions are callable
  • Classes are callable
  • Methods are callable
  • Instances of classes are callable

Example –

                    

class Car:
    def __init__(self, name):
        self.name = name

    def __call__(self):
        return f"{self.name} is a Luxury Car brand."

# Class is callable
print(callable(Car))

# Creating an instance
brand = Car('Audi')

# Function inside class is callable
print(callable(brand.__init__))

# Instance is callable
print(callable(brand))

Output –

                    

True
True
True

Object appears callable but is not callable

Let us revise the Note previously mentioned and understand it by looking at an example.

Note – Even if the callable() function returns TRUE, it is not necessary that the object will always be callable, it may fail too. However, if the function returns FALSE, then the object will be definitely not callable.

Example –

                    

class Display:
  def printStat(self):
     print('Hello World')

print('Is class Display callable? -', callable(Display))

InstanceOfDisplay = Display()
# Raises an Error 'Display' object is not callable
InstanceOfDisplay()

Output –

                    

Is class Display callable? - True
Traceback (most recent call last):
File "", line 9, in 
TypeError: 'Display' object is not callable

Frequently Asked Questions

Q1. What is callable in Python?

Objects in Python need to be fetched in order to be executed and used in the program. This process of fetching the objects is known as a call. When we say that we call a function, a class, a method inside a class, etc., we mean that we are fetching the values to perform the defined functionalities and display a result in return.

In Python, not all objects are callable. Few cannot be called. Python provides a built-in function, to determine which objects are callable. The function is callable(). If the object passed as a parameter to the callable() prints TRUE then that object is callable. If it displays FALSE then it is not callable in Python.

Q2. What is the use of __call__ in Python?

Python provides a built-in method known as __call__ method which allows the programmer to write classes where instances behave like a function and can be revoked/called like a function. Any object can be transformed into a callable object by simply writing it in the function call syntax __call__. One thing to bear in mind is that the object is utilized as a function, thus the syntax must be correct.

When the instance is called as a function; x(args1, args2, ….) simply denotes x.__call__(args1, args2, …).

Example –

                    

class Animal:
    def __call__(self):
        print('Dog is an animal.')

# create instance
x = Animal()

# call instance of a class
x()   # denotes x.__call__()

# check if function inside class is callable
print('Is function inside class callable? - ', callable(x.__call__))

Output –

                    

Dog is an animal.
Is function inside class callable? -  True

Q3. Are classes callable in Python? How do you call them?

Specific objects in Python can be called with the help of Python’s built-in function callable(). The callable() function accepts only a single parameter. The syntax for calling any object is –

                    

callable(object)

Few objects which are callable are:

  • Functions are callable
  • Classes are callable
  • Methods are callable
  • Instances of classes are callable

Example –

                    

# Python program to call a class
class Animal:
    def Message(self):
        print('All animals require love')

print('Is class Animal callable? - ', callable(Animal))

Output –

                    

Is class Animal callable? -  True

As per the above program example, we see that class ‘Animal’ is callable when we pass the object as a parameter to the Python’s callable() function.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.