Skip to content

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:

Language: 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.

Step through process_user_input(greet) to see greet go in as an uncalled function object and run only when process_user_input() calls it:

Interactive diagram — enable JavaScript to view.

The widget opens on step 5 of 7, with greet() running. Click Reset to start at the call, then click Step to move forward or Back to move backward. The pointer marks the running line, and the call stack shows each frame and its names, with the newest frame at the bottom. The violet chip is the one greet function object, so greet and callback both point to it.

Notice that greet() gets no frame of its own until callback(name) runs. The terminal shows what the program prints, and the underlined Pythonista is what the user types.

Defining Your Own Python Function

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:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Oct. 21, 2026