Python Callback Function | Explained

In Python, when the defined function is passed as a parameter value to another function, then this function is known as a “callback function”. This callback function is called at a specific point inside the main function. We can also access multiple callback functions inside the main function. The callback function helps us separate functions’ functionality and make code more reusable and modular.

In this blog, we will provide you with a complete guide on the Python callback function with multiple examples:

Example 1: Passing Callback Function as an Argument to Main Function

In the example given below, the “Callback Function” is defined and passed as an argument to the main function:

Code:

def func(cb):
    print("Hello!")
    cb()
    print("Good Work!")

def CBF():
    print("Callback function is called")
   
func(CBF)

In the above code:

  • The main function “func” and the callback function named “CBF” are defined in the program.
  • In the body of the main function, the “cb()” is used to access the callback function. The callback function is passed as an argument to the main function.

Output:

The above output shows the result generated by the main function and the callback function.

Example 2: Passing Multiple Arguments Along With Callback Function to Main Function

The callback function is also passed along with multiple arguments to the main function. Let’s understand it using the below code:

Code:

def func(cb, x, y):
    print("Python")
    cb(x,y)
    print('Multiplication of the Given Number is: ', x*y)

def CBF(x,y):
    z = x+y
    print('Sum of the Given Number is: ',z)

func(CBF, 55, 5)

In the above code:

  • The callback functions named “CBF”, “55” and “5” are passed as arguments to the main function.
  • When the main function is accessed, the callback function value inside the main function is also displayed on the screen.

Output:

The above output shows the value of the callback function when the main function is accessed.

Example 3: Passing Multiple Callback Functions as an Argument to Main Function

In the code given below, we passed multiple callback functions as an argument to the main function with the help of the list:

Code:

def func(cb, x, y):
    print("Hello_Python")
    for cb in cb_list:
        cb(x,y)
    print('Multiplication of the Given Number is: ', x*y)

def CBF(x,y):
    z = x+y
    print('Sum of the Given Number is: ',z)

def CBF_1(x,y):
    z = x/y
    print('Division of the Given Number is: ',z)
   
cb_list = [CBF, CBF_1]
func(CBF, 55, 5)

In the above code:

  • The two callback functions named “CBF” and “CBF_1” are defined in the program.
  • The list containing the function name of the callback function is initialized in the program.
  • In the main function, the for loop is used to iterate over the list and access the callback function value.

Output:

The above output verified that both callback function values are executed inside the main function.

Conclusion

A “callback function” in Python is a user-defined function that is input as a parameter value to the main function. The callback function is called at a specific point in the main function. To call multiple callback functions into the main function, we create the list of callback functions and iterate the list using a for loop to access. This post demonstrated various examples of Python callback functions with explanations.