New course! Every coder should learn Generative AI!
Try a free lesson+ 1
A callback is a function. In Python, functions are objects, so the name of a function can be used as a variable to another function.
def add():
...
anotherfunction(add)
Functions which accept a callback(function) as an argument usually require the arguments which callback accept.
def add(a, b):
return a+b
def main(a, b, _callback=None):
print("adding {} + {}".format(a, b))
if _callback:
print("Result : ", _callback(a , b))
main(1, 2, add)
Here, I am passing add to function main. Main is called the call back function.