python内置类型的比较顺序

2024-06-26 08:56:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我在寻找一组很好的哨兵值,例如实验:

[] > "sdfasfg" > ()

这似乎有记载:

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. reference

以同样的方式

^{pr2}$

“最大”的内置类型似乎是unicode,我可以用什么作为与unicode类型比较的“正确”哨兵值?这个值比任何unicode字符串都大?在

在Python中,内置类型实例的比较顺序是否定义得很好?在


Tags: of类型byobjectsunicodecpython内置are
1条回答
网友
1楼 · 发布于 2024-06-26 08:56:14

对任意对象的比较称为“丰富的比较”。有一些文档here,和{a2}。在

如果您使用的是python3,那么检查这个out。相关引述:

Having both the rich comparison methods and the __cmp__() method violates the principle that there should be only one obvious way to do it, so in Python 3 the support for __cmp__() has been removed. For Python 3 you therefore must implement all of the rich comparison operators if you want your objects to be comparable.

相关问题 更多 >