简体   繁体   English

python内置类型的比较顺序

[英]Comparison ordering of python built-in types

I'm looking for a good set of sentinel values, eg experimentally: 我正在寻找一套不错的哨兵值,例如实验性地:

[] > "sdfasfg" > ()

And this seems to be documented: 似乎已记录在案:

CPython implementation detail: Objects of different types except numbers are ordered by their type names; CPython实现细节:除数字外,其他类型的对象按其类型名称排序; objects of the same types that don't support proper comparison are ordered by their address. 不支持正确比较的相同类型的对象按其地址排序。 reference 参考

In the same fashion, 以同样的方式

object() < str()

The "largest" builtin type seems to be unicode , what can I use as a "right" sentinel value for comparison with unicode types? “最大”内置类型似乎是unicode ,我可以将哪些用作与unicode类型进行比较的“正确”哨兵值? That is a value that's larger than any unicode string? 那是一个大于任何Unicode字符串的值吗?

Is comparison order of instances of built-in types really well defined in Python? 内置类型实例的比较顺序在Python中是否定义正确?

The comparison of arbitrary objects is called "rich comarisons". 任意对象的比较称为“丰富比较”。 There is some documentation here , and here . 这里这里都有一些文档。

If you are working with Python 3, then check this out . 如果您使用的是Python 3,请检查一下 Relevant quote from this source: 此来源的相关报价:

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. 同时拥有丰富的比较方法和__cmp__()方法都违反了应该只有一种显而易见的方法,因此在Python 3中,对__cmp__()的支持已被删除。 For Python 3 you therefore must implement all of the rich comparison operators if you want your objects to be comparable. 因此,对于Python 3,如果希望对象具有可比性,则必须实现所有丰富的比较运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM