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.


import_contacts

You may also like:

Related Posts

  • Pandas apply function to column
    12 January

    Pandas apply function to column

    Table of ContentsHow do I apply function to column in pandas?Using dataframe.apply() functionUsing lambda function along with the apply() functionUsing dataframe.transform() functionUsing map() functionUsing NumPy.square() function We make use of the Pandas dataframe to store data in an organized and tabular manner. Sometimes there, is a need to apply a function over a specific column […]

  • Copy file Python
    12 January

    How to Copy File in Python?

    Table of ContentsCopy file using the shutil module in PythonCopy file using the copyfileobj() function in PythonCopy file using the copyfile() function in PythonCopy file using the copy() function in PythonCopy file using the copy2() function in PythonCopy file using the operating system command in PythonCopy file using the os module in PythonCopy file using […]

  • What is percent in python
    11 January

    What is % in Python?

    Table of ContentsWhat is % Operator in Python?The % Operator with strings in pythonThe % Operator as a placeholder for variables  in pythonThe % Operator as format specifiers in pythonConclusion In this article, we will cover what is % in Python and what are different ways to use % in Python. In python, there are […]

  • 09 January

    Convert set to String in Python

    Table of ContentsWays to convert set to string in PythonUsing the str() functionUsing the repr() functionUsing the join() functionUsing the map() functionConclusion Sets are an unordered collection of elements in Python. It does not contain duplicate elements and can be defined using the set() function in Python. Ways to convert set to string in Python […]

  • CharAt in Python
    08 January

    CharAt in Python

    Table of ContentsCharAt() in javaAlternative of charAt() in PythonIterate through String and print its character In this tutorial, we will see about CharAt in Python. If you are already well verse in java and might be looking for charAt() alternative in Python, then you are at right place. In Python, you can get character of […]

  • Convert PDF to text in Python
    07 January

    Convert PDF to Text in Python

    Table of ContentsConvert pdf to text using pypdf2Convert pdf to text using PDFminerConclusion Python is a feature-rich programming language. We can perform different operations on files in python using the different modules and libraries. In this article, we will discuss different ways to convert a pdf file to text in python. Convert pdf to text […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.