Callback functions in Python – A Complete Overview

Callback In Python Cover Image

A callback is a general concept in Python as well as other languages like Javascript, C, etc. We know that Python is an object-oriented language and functions are first-class objects in Python. This means, in Python, we can assign the value returned by a function to a variable and return a function from another function.

In this tutorial, we will learn about the concept of callback in Python programming language and we will also see a few examples related to it. So let’s get started.

Callback functions in Python

When one function is called from another function it is known as a callback. A callback function is a function that is passed to another function as an argument. It can be done in two ways:

  1. Passing one function as an argument to another function
  2. Calling a function inside another function

The above two points can help you decide if a given function is a callback function or not. It is not necessary that both the above conditions be true for a function to be a callback function. Even if one of the above two conditions is satisfied, the function is termed a callback function.

Callbacks can be implemented using both built-in functions as well as user-defined functions. We will be seeing examples for both implementations in this tutorial.

Callback using built-in functions

We are all familiar with the sorted function in Python. Let’s just have a quick recap of it first.


sorted(iterable, key)
iterable (mandatory)Data structure that is to be sorted (List, dictionary, etc.)
key (keyword, optional)Criteria for sorting. It can be assigned any function that you want to use.
sorted function parameters

When the sorted function is called, the iterable will be first passed to the key function and it will implement on the iterable. Then the new/changed iterable will be sorted.

Here, we are calling the key function inside the sorted function. So, the second condition is satisfied here. Hence, the key function here is a callback function.

Let’s see an example. Suppose we have to sort a list of strings.

a = ["lmn", "AbCd", "khJH", "ert", "SuNnY"]

#sorted based on the ASCII values

#first convert all letters to lowercase & then sort based on ASCII values
print(sorted(a, key=str.lower))


['AbCd', 'SuNnY', 'ert', 'khJH', 'lmn']
['AbCd', 'ert', 'khJH', 'lmn', 'SuNnY']

If you recall, the ASCII value for ‘A’ is 65, ‘B’ is 66, ‘e’ is 101 and so on. Please refer to this link for the ASCII values table.
So, in the first case, the list is sorted based on the ASCII values only.

If we observe the list, we can see that the strings inside the list are a mixture of uppercase as well lowercase alphabets. Let’s say, we now want to sort the list based on only the lowercase form of each letter. For this purpose, we have introduced a new parameter in the function key=str.lower.

Here, the str.lower is first implemented on the iterable list. That is, the strings in the list are converted into lowercase. So now, the list actually is 'lmn', 'abcd', 'khjh', 'ert', 'sunny' and this list is then passed to the sorted function.

Notice that the all lowercase list is different from the original list, but here ‘lmn’ represents ‘lmn’, ‘abcd’ represents ‘AbCd’, ‘khjh’ represents ‘khJH’, ‘ert’ represents ‘ert’ and ‘sunny’ represents ‘SuNnY’ in the original list. The sorted version of the new list is 'abcd', 'ert', 'khjh', 'lmn, 'sunny', hence the output is the one shown above.

So, this is how callback works in built-in functions in Python.

Callback in user-defined functions

Let us now learn about callbacks in user-defined functions. Let the function that ‘calls’ a function has the name caller and the one being called have the name called. Here, the caller function can be said to be a master and the called function as a slave, as the master function can always call the slave function but not vice-versa.

Suppose we are given a tuple consisting of two integers and we need to calculate the multiplication of these two integers.

def called(n):
    return n[0]*n[1]

def caller(func, n):
    return func(n)

num = (8, 5)

ans = caller(called, num)

print("Multiplication =", ans)


Multiplication = 40

In the above code, we have defined two functions: caller and called. The called function is passed as an argument to the caller function. Hence, the first condition is satisfied here, so this is also a case of callback and called is the callback function here.

When the program is executed, first, the tuple is created then the caller function is called. Now, the control goes to the definition of the caller function. It takes two arguments: a function (func) and an iterable (n), which is a tuple in this case. In caller function, it is desired that the function passed as argument is applied on the iterable and the result (func(n)) be returned.

So now, the control is at the called function definition. It takes a tuple as an argument and returns the multiplication of its members. This value is then returned to the caller function and then the caller function returns the answer i.e. 8×5 = 40.


Hence, we have learned about callbacks in Python and also how they are implemented using both built-in as well as user-defined functions.