python - python内置类型的比较排序

标签 python comparison built-in-types

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

[] > "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

以同样的方式,

object() < str()

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

Python 中内置类型实例的比较顺序真的定义得很好吗?

最佳答案

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

如果您使用的是 Python 3,请检查此 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.

关于python - python内置类型的比较排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17408362/

相关文章:

python - PIP 安装无法找到 ffi.h,即使它识别 libffi

用于 XSS 过滤的 Python 库?

Powershell:两个 System.Object 变量具有相同值的相等条件为 false

database - Prolog 数据库查询

dictionary - 在 Freemarker 中合并 map

c++ - 内置类型是否具有 move 语义?

javascript - 其他内置类型的类似方法,如 Array.IsArray

python - 比较两个 numpy 数组是否符合两个条件

python - np.random.seed() 和 np.random.RandomState() 之间的区别

floating-point - 将 IEEE-754 16 位 float 打包为 16 位无符号整数,同时保留顺序