callback

In Python, a callback is a function that you pass as an argument to another function. The receiving function can then call the callback at a later point, often as a response to an event or after completing a task.

Callbacks are a powerful way to customize the behavior of functions and are commonly used in asynchronous programming, event handling, and GUI applications.

Example

Here’s an example of a callback function in Python:

Python
>>> def greet(name):
...     print(f"Hello, {name}!")
...

>>> def process_user_input(callback):
...     name = input("Enter your name: ")
...     callback(name)
...

>>> process_user_input(greet)
Enter your name: Pythonista
Hello, Pythonista!

In this example, greet() is a callback function that gets called by process_user_input(). When the user enters their name, process_user_input() calls greet(), which then prints a greeting message.

Tutorial

Defining Your Own Python Function

In this tutorial, you'll learn how to define and call your own Python function. You'll also learn about passing data to your function, and returning data from your function back to its calling environment.

basics python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated April 10, 2025 • Reviewed by Leodanis Pozo Ramos