字典 __gt__ 和 __lt__ 实现
Dictionary __gt__ and __lt__ implementation
我一直在试验 Python 字典,发现 __gt__
和 __lt__
是为字典实现的。
我已经对它们进行了测试,它们似乎以某种方式比较了密钥,但我不太清楚这是如何完成的;例如,我不太确定 {1: 1} > {'0': 0}
returns False
(事实上,'0' > 100000
returns True
也是如此) .
这两个功能的具体实现有文档吗?
文档有 section on comparisons。特别是:
Objects of different types, except different numeric types and different string types, never compare equal; such objects are ordered consistently but arbitrarily (so that sorting a heterogeneous array yields a consistent result).
类似行为的原因:
>>> '0' < 0
False
>>> 0 < '0'
True
在CPython中是选择的"consistently but arbitrarily"比较方法是按类型名字母顺序排序,'str' > 'int'
:
CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.
这种行为是 altered for Python 3.x,您不能再比较异构类型(或字典,就此而言):
>>> '0' > 0
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
'0' > 0
TypeError: unorderable types: str() > int()
>>> {'a': None} > {'b': None}
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
{'a': None} > {'b': None}
TypeError: unorderable types: dict() > dict()
具体而言,字典的排序方式如下:
d1 > d2
变为:
(len(d1) > len(d2) or
(len(d1) == len(d2) and
sorted(d1.items()) > sorted(d2.items()))
(你可以在 CPython source code 中看到这个实现)。因此,如果它们的长度不同,"longer" 一个是 "larger":
>>> {1: 2, 3: 4} > {1: 2}
True
如果它们有匹配的键,则具有 "larger" 值的是 "larger":
>>> {1: 2} > {1: 1}
True
如果他们有不匹配的键,带有 "larger" 键的是 "larger":
>>> {1: 2} > {2: 1}
False
我一直在试验 Python 字典,发现 __gt__
和 __lt__
是为字典实现的。
我已经对它们进行了测试,它们似乎以某种方式比较了密钥,但我不太清楚这是如何完成的;例如,我不太确定 {1: 1} > {'0': 0}
returns False
(事实上,'0' > 100000
returns True
也是如此) .
这两个功能的具体实现有文档吗?
文档有 section on comparisons。特别是:
Objects of different types, except different numeric types and different string types, never compare equal; such objects are ordered consistently but arbitrarily (so that sorting a heterogeneous array yields a consistent result).
类似行为的原因:
>>> '0' < 0
False
>>> 0 < '0'
True
在CPython中是选择的"consistently but arbitrarily"比较方法是按类型名字母顺序排序,'str' > 'int'
:
CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.
这种行为是 altered for Python 3.x,您不能再比较异构类型(或字典,就此而言):
>>> '0' > 0
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
'0' > 0
TypeError: unorderable types: str() > int()
>>> {'a': None} > {'b': None}
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
{'a': None} > {'b': None}
TypeError: unorderable types: dict() > dict()
具体而言,字典的排序方式如下:
d1 > d2
变为:
(len(d1) > len(d2) or
(len(d1) == len(d2) and
sorted(d1.items()) > sorted(d2.items()))
(你可以在 CPython source code 中看到这个实现)。因此,如果它们的长度不同,"longer" 一个是 "larger":
>>> {1: 2, 3: 4} > {1: 2}
True
如果它们有匹配的键,则具有 "larger" 值的是 "larger":
>>> {1: 2} > {1: 1}
True
如果他们有不匹配的键,带有 "larger" 键的是 "larger":
>>> {1: 2} > {2: 1}
False