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

Learn how to define your own Python function, pass data into it, and return results to write clean, reusable code in your programs.

basics python

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


By Leodanis Pozo Ramos • Updated April 10, 2025