You might have heard that functions in python are callable objects. In this article, we will discuss what exactly we mean by the term callable object. We will discuss concepts behind the implementation of a callable object and will implement programs to demonstrate the use of callable objects in python.
What is meant by calling an object?
We call any object by placing round brackets after them. For example, When we have to call a function, we place round brackets after them as follows.
def add(num1, num2):
value = num1 + num2
return value
val = add(10, 20)
print("The sum of {} and {} is {}".format(10, 20, val))
Output:
The sum of 10 and 20 is 30
Here, we have called the add() function by passing 10 and 20 as input parameters. The function returns the sum of the input numbers after execution.
In a similar way, we can also call other callable objects. But, if we call an object that is not callable, the python interpreter will raise the TypeError exception with a message that the object is not callable. This can be observed as follows.
val = 10
val()
Output:
Traceback (most recent call last):
File "/home/aditya1117/PycharmProjects/pythonProject/webscraping.py", line 2, in <module>
val()
TypeError: 'int' object is not callable
Here, you can see that we have defined an integer variable and then we have called it. Upon execution, It raises TypeError exception with a message that “int” object is not callable.
What is the reason that calling a function is okay but calling an integer variable raise exception? Let’s find out.
What are callable objects in Python?
A Callable object in python is such an object that executes some code on being called instead of raising a TypeError.
Every callable object has the __call__() method implemented inside its class definition. If we define callable objects using this detail, then a Callable objects in python are those objects that have an implementation of the __call__() method in their class definition.
If an object does not have the implementation of the __call__() method in its class definition, it will raise a TypeError exception whenever it is called. This can be seen in the following example.
class Website:
def __init__(self):
self.name = "Python For Beginners"
myWebsite = Website()
myWebsite()
Output:
Traceback (most recent call last):
File "/home/aditya1117/PycharmProjects/pythonProject/webscraping.py", line 7, in <module>
myWebsite()
TypeError: 'Website' object is not callable
Here the object myWebsite does not have the implementation of the __call__() method in the definition of its class Website. So, it raises the TypeError exception with a message that the ‘Website’ object is not callable when it is called.
Now let us implement the __call__() method in the Website class that prints the address of the website. Observe the output here.
class Website:
def __init__(self):
self.name = "Python For Beginners"
def __call__(self, *args, **kwargs):
print("Called me?")
print("I am available at pythonforbeginners.com")
myWebsite = Website()
myWebsite()
Output:
Called me?
I am available at pythonforbeginners.com
Now, it might be clear to you that we can call any object that has an implementation of the __call__() method in its class definition.
How can we create callable objects in python?
We have seen above that all the callable objects have the implementation of the __call__() method in their class definition. So, To create a callable object in python, we will implement the __call__() method in the function definition of the object as seen in the example given abve.
class Website:
def __init__(self):
self.name = "Python For Beginners"
def __call__(self, *args, **kwargs):
print("Called me?")
print("I am available at pythonforbeginners.com")
myWebsite = Website()
myWebsite()
Output:
Called me?
I am available at pythonforbeginners.com
Conclusion
In this article, we have discussed callable objects in python. We also discussed how we can create a callable object using the __call__() method. To learn more about python programming, you can read this article on list comprehension. You may also like this article on the linked list in Python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.