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 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

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

Boolean Operators and Short-Circuiting

Fully understand Python's boolean operators for negation, conjunction, and disjunction. Master operator precedence and short-circuiting.

By Christoph Schiessl on Python

Christoph Schiessl

Hi, I'm Christoph Schiessl.

I help you build robust and fast web applications.


I'm available for hire as a freelance web developer, so you can take advantage of the more than a decade of experience I have collected working on many projects across several industries. Most of my clients are building web-based SaaS applications in a B2B context and depend on my expertise in various capacities.

More often than not, my involvement includes hands-on development work using technologies like Python, JavaScript, and PostgreSQL. Furthermore, if you already have an established team, I can support you as a technical product manager with a passion for simplifying complex processes. Lastly, I'm also a skilled writer who takes pride in breaking down technical concepts into the simplest possible terms.