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?


import_contacts

You may also like:

Related Posts

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

  • 11 June

    Check if Object Is Iterable in Python

    Table of ContentsWhat are iterables in Python?How to check if object is iterable in Python?Using the iter() function to check if object is iterable in PythonUsing the for loop to check if object is iterable in PythonUsing the isinstance() function to check if object is iterable in PythonConclusionWas this post helpful? In this post, we […]

  • 11 June

    Save Numpy Array as Image in Python

    Table of ContentsSave numpy array as image in PythonHow to save numpy array as image in Python?Using the PIL library to save numpy array as image in PythonUsing the matplotlib.image library to save numpy array as image in PythonUsing the opencv library to save numpy array as image in PythonUsing the imageio library to save […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.