Table of Contents
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
def sumofNumbers(N): output = 0 for i in range(1, N + 1): output = output + i print("The sum of numbers from 1 to {} is {}.".format(N, output)) def factorial(N): output = 1 for i in range(1, N): output = output * i print("The factorial of {} is {}.".format(N, output)) N = int(input("Enter a number:")) if N % 2 == 0: factorial(N) else: sumofNumbers(N) |
Output:
1 2 3 4 |
Enter a number:15 The sum of numbers from 1 to 15 is 120. |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
def sumofNumbers(N): output = 0 for i in range(1, N + 1): output = output + i print("The sum of numbers from 1 to {} is {}.".format(N, output)) def factorial(N): output = 1 for i in range(1, N): output = output * i print("The factorial of {} is {}.".format(N, output)) def operate(N, func): func(N) N = int(input("Enter a number:")) if N % 2 == 0: operate(N, factorial) else: operate(N, sumofNumbers) |
Output:
1 2 3 4 |
Enter a number:15 The sum of numbers from 1 to 15 is 120. |
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.
Further reading:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Laptop: def __init__(self, name, ram): self.name = name self.RAM = ram def __str__(self): return self.name + " RAM= " + str(self.RAM) + "GB" myLaptop1 = Laptop("laptop1", 16) myLaptop2 = Laptop("laptop12", 32) myLaptop3 = Laptop("laptop22", 64) myLaptop4 = Laptop("laptop124", 4) myLaptop5 = Laptop("laptop34", 8) myList = [myLaptop1, myLaptop2, myLaptop3, myLaptop4, myLaptop5] print("The laptops in the list are:") for laptop in myList: print(laptop) |
Output:
1 2 3 4 5 6 7 8 |
The laptops in the list are: laptop1 RAM= 16GB laptop12 RAM= 32GB laptop22 RAM= 64GB laptop124 RAM= 4GB laptop34 RAM= 8GB |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class Laptop: def __init__(self, name, ram): self.name = name self.RAM = ram def __str__(self): return self.name + " RAM= " + str(self.RAM) + "GB" def getValue(laptop): return laptop.RAM myLaptop1 = Laptop("laptop1", 16) myLaptop2 = Laptop("laptop12", 32) myLaptop3 = Laptop("laptop22", 64) myLaptop4 = Laptop("laptop124", 4) myLaptop5 = Laptop("laptop34", 8) myList = [myLaptop1, myLaptop2, myLaptop3, myLaptop4, myLaptop5] print("The laptops in the input list are:") for laptop in myList: print(laptop) myList.sort(key=getValue) print("The laptops in the output list are:") for laptop in myList: print(laptop) |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
The laptops in the input list are: laptop1 RAM= 16GB laptop12 RAM= 32GB laptop22 RAM= 64GB laptop124 RAM= 4GB laptop34 RAM= 8GB The laptops in the output list are: laptop124 RAM= 4GB laptop34 RAM= 8GB laptop1 RAM= 16GB laptop12 RAM= 32GB laptop22 RAM= 64GB |
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.