Callback functions in Python – A Complete Overview
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:
- Passing one function as an argument to another function
- 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.
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.
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.
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.
IMAGES
VIDEO
COMMENTS
The Python return statement is a special statement that you can use inside a function or method to send the function’s result back to the caller. A return statement consists of the return keyword followed by an optional return value.
For example: def power_function(power): return lambda x : x**power. power_function(3)(2) This returns 8. power_function is a function that returns a function as …
In this tutorial, you'll learn how to use Python's assignment operators to write assignment statements that allow you to create, initialize, and update variables in your code.
The return statement in Python is used to exit a function and return a value to the caller. It allows you to pass back a specific result or data from a function, enabling you to utilize the output for further computation or processing.
A return statement is used to end the execution of the function call and “returns” the result (value of the expression following the return keyword) to the caller. The statements after …
How to define and call a function in Python. In Python, functions are defined using def statements, with parameters enclosed in parentheses (), and return values are indicated …
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 …