How does using a function (callback) as an argument to another function work in Python?

Yes, this is possible. myfunc can call the passed-in function like:

def myfunc(anotherfunc, extraArgs):
    anotherfunc(*extraArgs)

Here is a fully worked example:

>>> def x(a, b):
...     print('a:', a, 'b:', b)
... 
>>> def y(z, t):
...     z(*t)
... 
>>> y(x, ('hello', 'manuel'))
a: hello b: manuel

Leave a Comment