The meaning of "callable" in Python

Share
Copied to clipboard.
Series: Decorators
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
3 min. read 2 min. video Python 3.8—3.12
Python Morsels
Watch as video
02:04

What is a "callable" in Python?

Python's functions are callable objects

When you define a function in Python:

>>> def greet(name):
...     print("Hi", name)
...

You'll get a function object:

>>> greet
<function greet at 0x7f61693c5940>

You can call a function object by putting parentheses (()) after it:

>>> greet("Trey")
Hi Trey

So, every function in Python is a callable, meaning it's an object that you're able to call.

Is my callable a function or a class?

Functions are not the only callables in Python.

If we call the built-in enumerate function, we will get back an enumerate object:

>>> colors = ["green", "purple", "white", "pink"]
>>> enumerate(colors)
<enumerate object at 0x7f816c0ad400>

We can loop over this enumerate object to see what's in it. Looping is really the one thing we can do with an enumerate object (more on iterating through lists with indexes in Python).

>>> list(enumerate(colors))
[(0, 'green'), (1, 'purple'), (2, 'white'), (3, 'pink')]

When we call enumerate, we get back an enumerate object. It turns out, enumerate isn't actually a function: it's a class!

>>> enumerate
<class 'enumerate'>

We call enumerate a function because we often use the word "function" in a fuzzy way in Python.

In Python we think in terms of duck typing. So if something is a function-like object (meaning it acts like a function) we may simply call it "a function".

The built-in enumerate function acts like a function (you call it using parenthesis) which means it's basically a function. But more properly, enumerate is a callable, meaning it's an object that can be called.

In Python, functions can be called and classes can be called. So both functions and classes are callables in Python.

But functions and classes aren't the only callables in Python.

Making callable class instances

Functions and classes are both callables, but you can actually invent your own callable objects too.

We have a class here called Person:

class Person:
    def __init__(self, name):
        self.name = name
    def __call__(self):
        print("My name is", self.name)

Classes are callable and we call the Person class to get back an instance of that class:

>>> trey = Person("Trey")
>>> trey
<__main__.Person object at 0x7fbf9f3331c0>

But the Person object is also callable! We can call that Person object by putting parentheses after it:

>>> trey()
My name is Trey

This works because we've implemented a __call__ method on the Person class. Adding a __call__ method to a class makes its class instances callable.

In fact, anything with a __call__ is a callable and all callables have a __call__ method. Even functions and classes have a __call__ method (that's how they're callable):

>>> greet.__call__
<method-wrapper '__call__' of function object at 0x7fbd631cd940>
>>> enumerate.__call__
<method-wrapper '__call__' of type object at 0x56322b9cb580>

Functions, classes, and objects with __call__ are callables

In Python, a callable is a function-like object, meaning it's something that behaves like a function. Just like with a function, you can use parentheses to call a callable.

Functions are callables in Python but classes are callables too! These are not the only callables, but they're the primary two types of callables that you'll most often see in Python.

For more on callables in Python, see Python's functions are sometimes classes.

A Python Tip Every Week

Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.

Python Morsels
Watch as video
02:04