Callback functions in Python

Free Python Code - Aug 23 '23 - - Dev Community

Hi 🙂🖐

Frist we need to kenow what is callback function

A callback function is a function that is passed to another function as an argument

examples


def myFunc():
    print('from myFunc function')


def call_func(callback_function):
    callback_function()


call_func(myFunc)

Enter fullscreen mode Exit fullscreen mode

As you can see, call_func takes another function as an argument.

another example


def welcome1(name):
    print(f'welcome {name}')


def welcome2(name):
    print(f'Welcome : {name}')


def check_user(name, callback):
    if name == 'amr':
        callback(name)


check_user('amr', welcome1)
check_user('amr', welcome2)

Enter fullscreen mode Exit fullscreen mode

In this example you can modify the function you are passing to another function, which is one of the advantages. of callback functions

For more info
https://www.askpython.com/python/built-in-methods/callback-functions-in-python

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .