0

What is the role of function cmp_to key from funtools module

What is the role of function cmp to key, when this is user often?

3rd Jan 2020, 8:43 PM
Frontend Dev Junior
Frontend Dev Junior - avatar
3 odpowiedzi
+ 1
* when this is used?
3rd Jan 2020, 8:43 PM
Frontend Dev Junior
Frontend Dev Junior - avatar
0
I haven't seen any need to use this yet. cmp_to_key is more of a utility tool for Python 2 backwards compatibility. For sorting or comparing values, python 2 used the concept of comparison functions, that have two input values and return negative, zero or positive number. Python 3 uses key functions for sorting, these have only a single argument and associates a (sorting) key to each value. This function cmp_to_key is supposed to bridge this difference. https://docs.python.org/3.8/library/functools.html
4th Jan 2020, 5:56 AM
Tibor Santa
Tibor Santa - avatar
0
Thanks
4th Jan 2020, 1:48 PM
Frontend Dev Junior
Frontend Dev Junior - avatar