Skip to content

Python callback function with arguments

  • by

Python callback function with arguments is simple and required to do its purpose. First understand callback function is a function that is passed as an argument to another function and is called back later in the program’s execution.

def callback_function(arg1, arg2):
    print(arg1, arg2)

Python callback function can have one or more arguments as input and passes them to the callback function when it is called.

Python callback function with arguments example

Simple example code.

def callback_function(arg1, arg2):
print("Callback function called with arguments: \n", arg1, arg2)


def do_something(callback):
number = 100
callback(number, "Hello world!") # arg1 and arg2


do_something(callback_function)

Output:

Python callback function with arguments

Do comment if you have any doubts or suggestions on this Python callback function topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *