The following code block gives ['AAB', 'BBC', 'EFM', 'CNN', 'ESPN', 'ITN', 'XYZ'] as the output. But the MyCmp() function that is inside the sort() method does not have any parameters .If I created a function that takes exactly one parameter and did call it inside the sort() method, it gives an error message telling "TypeError: MyCmp() takes exactly 1 argument (2 given)"
Can anybody explain the reason for this? Why does that MyCmp() function does not need to be given any parameters like in a normal function call?
A = ['ESPN','BBC','AAB','XYZ','CNN','ITN','EFM']
def MyCmp(a,b):
return cmp(a[1],b[1])
A.sort(MyCmp)
print A
When I added print a,b line to the function. It outputs
BBC ESPN
AAB BBC
XYZ AAB
XYZ BBC
XYZ ESPN
CNN ESPN
CNN BBC
ITN CNN
ITN XYZ
ITN ESPN
EFM ESPN
EFM BBC
EFM CNN
When you do
A.sort(MyCmp), you're passingMyCmpas an argument toA.sort. You're not callingMyCmpat all yourself. Instead, that's something the implementation ofsortwill do while it runs. Because of howlist.sortworks, it will provide two arguments when it makes those actual calls.You can write your own code that treats functions as first-class objects. For instance, here's a function that calls a function you pass it as an argument twice (with no arguments):
There are lots of situations where it's handy to be able to pass functions around (callbacks, decorators). If you're just starting to learn Python, you may not need those things immediately, but do expect that you'll learn about them sooner or later.
Note that using a
cmpfunction for sorting is deprecated. In Python 3, the only way to customize a sort is by using akeyfunction (which takes only one argument). You can also use akeyfunction in the more recent versions of Python 2, so if you want to write forwards compatible code, you should go that route. Starting in Python 3.2, you can usefunctools.cmp_to_keyto create a key function from an oldercmpfunction. The docs offer an implementation of the function you can use in older Python versions. But I'd just stay away fromcmpin new code.