In Python, functions are first-class objects. You can pass functions as arguments to other functions and return them from functions. In this tutorial, you will learn the callback function in Python.
A function is a piece of code that performs some task and returns the result of the task. Functions can be called by using the call() method. The call() method takes a single argument, the function name, and passes the value of this function as an argument to the other function.
The syntax of the call() method is given below:
call(function_name, [arguments])
For example, if you want to call the function add() with the argument 1, then the code will be written as follows:
add(1)
The function can also be passed to other functions and used as a return value. To illustrate this, we can define a function sum().
def sum(x, y):
return x + y
We can use the function sum() inside another function as follows:
sum(2, 3)
Here, the call() method is used to call the function sum() and the result of the addition of 2 and 3 is returned to the caller.
Let us now try to understand the concept of callback functions. We will use the built-in function random.randint() to generate a random integer between 0 and 9.
def get_random_integer():
return random.randint(0, 9)
Now, we will create a function that will return the random integer generated by the above function. We will name it randint_generator() and we will pass the function get_random_integer() as the argument. The syntax of the call() method will be as follows:
randint_generator(get_random_integer)
Here, the call() method is used to call the function randint_generator() and the result of the generator is returned to the caller.
We will now make a small change to the function randint_generator() and print the output.
def randint_generator(get_random_integer):
return get_random_integer() * 10
Now, we will make another function that will call the above function and print the output.
def print_the_result():
print(randint_generator(get_random_integer))
Here, the call() method is used to call the function print_the_result() and the result is printed to the caller.
The above code will print a random integer between 0 and 9.
We use the callback function for any kind of event. For example, if we want to play a sound when the user clicks on the button, we can use the callback function for that. In this article, I will explain the concept of the callback function in python and its usage in web development.
First, let me tell you that the callback function is used for events, so it is nothing but an event handler. Suppose, we want to play a sound when a button is clicked, the below code will help us to perform that task.
from tkinter import *
root=Tk()
btn=Button(text="Click Me",width=30,height=15)
btn.grid(row=0,column=0)
def mycallback():
sound.play()
btn.bind('',mycallback)
sound=Sound("C:\Python\Sound.wav")
sound.play()
root.mainloop()
Now, when you click on the button, you will hear the sound.
This is the basic way to use the callback function. But, if we want to do more than one thing in the callback function, we need to use the if statement.
Example:
from tkinter import *
root=Tk()
btn=Button(text="Click Me",width=30,height=15)
btn.grid(row=0,column=0)
def mycallback():
if btn.cget("state")=="disabled":
print "Disabled"
else:
print "Enabled"
btn.bind('',mycallback)
sound=Sound("C:\Python\Sound.wav")
sound.play()
root.mainloop()
This was the basic example of the callback function. I have discussed the concept of the callback function and I hope you understood this topic well. This is a very basic concept of the programming. Now, you will be able to write a function and can call that function when the desired event is triggered.