cmp

Why is the cmp parameter removed from sort/sorted in Python3.0?

醉酒当歌 提交于 2019-11-27 07:24:55
from python wiki : In Py3.0, the cmp parameter was removed entirely (as part of a larger effort to simplify and unify the language, eliminating the conflict between rich comparisons and the __cmp__ methods). I do not understand the reasoning why cmp is removed in py3.0 consider this example: >>> def numeric_compare(x, y): return x - y >>> sorted([5, 2, 4, 1, 3], cmp=numeric_compare) [1, 2, 3, 4, 5] and now consider this version (recommended and compatible with 3.0): def cmp_to_key(mycmp): 'Convert a cmp= function into a key= function' class K(object): def __init__(self, obj, *args): self.obj =

Why is the cmp parameter removed from sort/sorted in Python3.0?

我是研究僧i 提交于 2019-11-26 13:05:42
问题 from python wiki: In Py3.0, the cmp parameter was removed entirely (as part of a larger effort to simplify and unify the language, eliminating the conflict between rich comparisons and the __cmp__ methods). I do not understand the reasoning why cmp is removed in py3.0 consider this example: >>> def numeric_compare(x, y): return x - y >>> sorted([5, 2, 4, 1, 3], cmp=numeric_compare) [1, 2, 3, 4, 5] and now consider this version (recommended and compatible with 3.0): def cmp_to_key(mycmp): \