The Built-in callable() Function

by Christoph Schiessl on Python

Another useful and interesting built-in function is callable(object), which the official documentation explains as follows:

Return True if the object argument appears callable, False if not. If this returns True, it is still possible that a call fails, but if it is False, calling object will never succeed. Note that classes are callable (calling a class returns a new instance); instances are callable if their class has a __call__() method.

This sounds easy enough, so let's try it out:

>>> def custom_function():
...     pass
...
>>> callable(custom_function)
True
>>> custom_lambda = lambda: None
>>> callable(custom_lambda)
True

Ordinary functions and lambdas are obviously callable. Everything else wouldn't make much sense, would it?

>>> callable(list)
True
>>> class CustomClass:
...     pass
...
>>> callable(CustomClass)
True

Both, built-in classes (e.g., list) and user-defined classes (e.g., CustomClass), are also callable. As a matter of fact, classes are always callable, and if you do call them, you get a new instance of the class as the return value.

>>> callable(list())
False
>>> callable([])
False
>>> class CustomClass:
...     pass
...
>>> callable(CustomClass)
True

Objects — aka instances of classes — are a different story. For example, instances of list are not callable. By the way, list() and [] are the same thing; both create a new instance of the list class, and in both cases, the newly created list object doesn't contain elements. In other words, the two new lists are empty.

>>> class CustomClass:
...     pass
...
>>> callable(CustomClass)
True
>>> callable(CustomClass())
False
>>> class CustomCallableClass:
...     def __call__(self):
...         pass
...
>>> callable(CustomCallableClass)
True
>>> callable(CustomCallableClass())
True

User-defined classes are not callable by default unless they define the dunder method __call__(), which is what the CustomCallableClass in the example above is doing.

You can also understand callable objects as follows: An object x = SomeClass() can be called as x() if and only if it is callable(x) == True. If you call an object like this, what's happening behind the scenes is roughly equivalent to type(x).__call__(x). This works because type(x) == SomeClass.

>>> class SomeClass:
...     def __call__(self): # Add parameters if you need to ...
...         print(f"{type(self).__name__}.__call__() has been called!")
...
>>> x = SomeClass()
>>> x()
SomeClass.__call__() has been called!
>>> type(x).__call__(x)
SomeClass.__call__() has been called!

Isn't this all fascinating? The point of this article was to demonstrate that everything in Python is potentially callable. Thank you for reading, and as always, please don't hesitate to reach out if you have any comments or questions.

Ready to Learn More Web Development?

Join my Mailing List to receive two useful Articles per week.


I send two weekly emails on building performant and resilient Web Applications with Python, JavaScript and PostgreSQL. No spam. Unscubscribe at any time.

Continue Reading?

Here are a few more Articles for you ...


The Built-in any() Function

Learn how to use the built-in any() function in Python to determine if any element in an iterable is True, with implementation and performance insights.

By Christoph Schiessl on Python

The Built-in bool() Class

Learn about boolean values in Python and the standard truth testing procedure. Understand how objects are converted to True or False.

By Christoph Schiessl on Python

Telling Docker Who You Are

Learn how to avoid permission issues when creating files on a Docker bind-mount volume from within a container and manage user IDs and group IDs on Linux.

By Christoph Schiessl on DevOps and Docker

Christoph Schiessl

Christoph Schiessl

Independent Consultant + Full Stack Developer


If you hire me, you can rely on more than a decade of experience, which I have collected working on web applications for many clients across multiple industries. My involvement usually focuses on hands-on development work using various technologies like Python, JavaScript, PostgreSQL, or whichever technology we determine to be the best tool for the job. Furthermore, you can also depend on me in an advisory capacity to make educated technological choices for your backend and frontend teams. Lastly, I can help you transition to or improve your agile development processes.