https://pythonexamples.org/python-callback-function/
def callbackFunc(s): print('Length of the text file is : ', s) def printFileLength(path, callback): f = open(path, "r") length = len(f.read()) f.close() callback(length) if __name__ == '__main__': printFileLength("sample.txt", callbackFunc)
def callbackFunc1(s): print('Callback Function 1: Length of the text file is : ', s) def callbackFunc2(s): print('Callback Function 2: Length of the text file is : ', s) def printFileLength(path, callback): f = open(path, "r") length = len(f.read()) f.close() callback(length) if __name__ == '__main__': printFileLength("sample.txt", callbackFunc1) printFileLength("sample.txt", callbackFunc2)
https://www.localsolver.com/docs/last/technicalfeatures/callbacks.html
class CallbackExample: def __init__(self): self.last_best_value = 0 self.last_best_running_time = 0 def my_callback(self, ls, cb_type): stats = ls.statistics obj = ls.model.objectives[0] if obj.value > self.last_best_value: self.last_best_running_time = stats.running_time self.last_best_value = obj.value if stats.running_time - self.last_best_running_time > 5: print(">>>>>>> No improvement during 5 seconds: resolution is stopped") ls.stop() else: print(">>>>>> Objective %d" % obj.value) ls = localsolver.LocalSolver() cb = CallbackExample() ls.add_callback(localsolver.LSCallbackType.TIME_TICKED, cb.my_callback)
https://www.askpython.com/python/built-in-methods/callback-functions-in-python
def called(n): return n[0]*n[1] def caller(func, n): return func(n) #tuple num = (8, 5) ans = caller(called, num) print("Multiplication =", ans)
https://www.delftstack.com/howto/python/python-callback-function/
def Func_CallBack(c): print("File Length : ", c) def File_Len(filepath, callback): i = open(filepath, "r") file_length = len(i.read()) i.close() callback(file_length) if __name__ == "__main__": File_Length("randomfile.txt", Func_CallBack)
https://stackoverflow.com/questions/40843039/how-can-i-write-a-simple-callback-function
def callback(n): print("Sum = {}".format(n)) def main(a, b, _callback = None): print("adding {} + {}".format(a, b)) if _callback: _callback(a+b) main(1, 2, callback)
https://www.ionos.fr/digitalguide/sites-internet/developpement-web/quest-ce-quun-callback/
def get_square(val): return val ** 2 def caller(func, val): return func(val) caller(get_square, 5)