Callback function in Python

Python callback function

In python, functions are first-class objects. In other words, we can pass a function to a different function as an input argument. We can return a function from another function, and we can assign a function to a variable. In this article, we will see what is a callback function in python and how we can create a callback function.

What is Callback Function in Python?

As a function in python is a first-class object, we can pass a function to another function as an input argument. In such cases, the function that is passed as the input argument to another function is called a callback function in Python.

For instance, suppose that you have to write a program that takes a number as input and prints its factorial if the input is an even number and prints the sum of numbers from 1 to the input number if it’s an odd number. 

You can do this by defining a function to calculate factorial and another function to calculate the sum of numbers as follows.

Output:

Now, suppose that you are not allowed to call the factorial() function and sumofNumbers() function in the main module. What would you do now?

In this case, we can define another function operate() that takes the input number and the function as input and executes the input function by calling the function with the input parameter as follows.

Output:

Here, You can observe that we are passing the factorial function and the sumofNumbers function as input to the operate() function. The operate() function then calls these functions to perform any operation. Hence, the factorial() function and the sumofNumbers() function are named callback functions because they are being passed to an another function and the function calls them to execute some task..

To conclude, a callback function in python is a function that is passed to another function as an input argument and is called by that function to perform some operation.

Applications of Callback Functions in Python

We generally use a callback function in python when we have to perform an operation before doing something. For instance, suppose that we have a list of objects and we have to sort them using an attribute of the object. In such cases, the sort() method or the sorted() function doesn’t work directly. Here, we can use a callback function to sort such a list. 

Let us consider that we have a list of Laptop objects. Each laptop has a name attribute and the amount of RAM as another attribute as shown below. 

Output:

We have to sort the list of Laptop objects using the attribute RAM. To sort the list, first, we will define a function getValue(). The getValue() function takes a laptop object as input and returns the amount of RAM in the laptop as output. 

With the list of objects, we will pass the getValue function to the sort() method as an input argument for the parameter “key” as shown in the following program.

Output:

 Here, the sort() method calls the getValue() function to obtain the value of RAM in each Laptop object while sorting the objects. Hence, the getValue() function is a callback function as we have passed it to the sort() method and it is being called inside the sort() method to perform some operation. 

You can use a callback function in python whenever you need to do extra work before performing some operation. To do the extra work, you can define a function and use it as a callback function as we did with the factorial() and sumofNumbers() function in the previous example and the getValue() in this example.

Conclusion

In this article, we have discussed what is a callback function in python. We saw that a callback function is a function that is passed as an argument to another function. This other function is expected to call this callback function in its definition. You may choose to use a callback function in your code depending on your convenience. However, using the callback function may increase the readability and reusability of your code.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *