• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Callable Objects in Python

Callable Objects in Python

Author: Aditya Raj
Last Updated: October 15, 2021

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.

Table of Contents
  • What is meant by calling an object?
  • What are callable objects in Python?
  • How can we create callable objects in python?
  • Conclusion

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.

Related

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.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us