Theres a lots of people treated python as scripting language like bash, but I am going to tell you, python is real programming language. Python support callback like c/c++, and this feature is really God-like, because it is so simple to implement callback in python.
What is Callback?
In computer programming, a callback is executable code that is passed as an argument to other code. It allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.
Quote from wikipedia
When we talk about callback, it usually in the form of function, therefore we also call it as callback function. We define our own algorithm in a callback function, and pass our function name as parameter to another function, where this function will callback our function for certain execution depends on the context.
Lets look at how we can apply callbacks in python. Lets look into how to define our own sorting function with callback technique.
Llist object support sorting by default, you can sort data in list as below:
In : mylist=[2,3,8,4,7,1] In : print mylist [2, 3, 8, 4, 7, 1] In : mylist.sort() In : print mylist [1, 2, 3, 4, 7, 8]
What if we have value in string?
In : mylist=['John', 'Thomas','Dick','Harry'] In : print mylist ['John', 'Thomas', 'Dick', 'Harry'] In : mylist.sort() In : print mylist ['Dick', 'Harry', 'John', 'Thomas']
But, look at this, it will fails to sort as expected:
In : mylist=["r1","r13","r3","r5","r100","r103"] In : mylist.sort() In : print mylist ['r1', 'r100', 'r103', 'r13', 'r3', 'r5']
What are we expect is ['r1','r3','r5','r13','r100','r103'] but end up sorted alphanumerically.
So, what should we do? create our own sorting function? how are we going to embed this sorting to the list object?
If you learn about algorithms before, you may understand that to perform sorting, we compare the every single value in a set, and thats how we sort it out. There are various sorting techniques, the simplest one is bubble sort.
You can use nested for loops to implement bubble sort, but here, you do NOT need to. You do not need create the entire sorting function yourself, you just need to define your own compare algorithms into callback function and pass it’s name to sort(), and python will sort accordingly for you.
Let us look at a sample user define compare callback:
def compare(x,y): if int(x[1:]) > int(y[1:]): return 1 elif int(x[1:]) < int(y[1:]): return -1 else: return 0
Next! lets see the results:
In : mylist.sort(compare) In : print mylist ['r1', 'r3', 'r5', 'r13', 'r100', 'r103']
To define a compare function for sort(), you must follow certain pattern.
1. Compare function must take TWO param: x and y,
2. It should return positive number if x > y, return negative number if x< y and return 0 if they are equal for Ascending sort.
So this is callback, the sort function will now call your compare callback in the sorting algorithm instead of the default one.
P.S. I always having difficulties to explain programming, my wish is to introduce callbacks for those who are learning python. Hope that this is useful for you.
FYI: In case you are curious why my python's prompt is so funny, I am using ipython, an Enhanced Python Shell.