一切都大于无吗? [英] Is everything greater than None?
问题描述
是否有 Python 内置数据类型,除了 None
,对于其中:
其中 foo
是那种类型的值?Python 3 怎么样?
None
总是小于 Python 2 中的任何数据类型(参见 object.c
).
在 Python 3 中,这被改变了;现在对没有合理自然排序的事物进行比较会导致 TypeError
.来自 3.0新功能"更新强>:
Python 3.0 简化了排序比较的规则:
排序比较运算符(<
、<=
、>=
、>
)当操作数没有有意义的自然顺序时,引发 TypeError
异常.因此,表达式如: 1 <;''
, 0 >None
或 len <= len
不再有效,例如无
TypeError
而不是返回 False
.一个推论是对异构列表进行排序不再有意义——所有元素必须相互比较.请注意,这不适用于 ==
和 !=
运算符:不同不可比较类型的对象总是相互不相等.
这让一些人感到不安,因为对包含一些 None
值的列表进行排序,并让 None
值聚集在开始或结束.有一个线程 不久前在邮件列表中提到过这个问题,但最终的一点是 Python 3 试图避免对排序做出武断的决定(这在 Python 2 中经常发生).
Is there a Python built-in datatype, besides None
, for which:
>>> not foo > None
True
where foo
is a value of that type? How about Python 3?
None
is always less than any datatype in Python 2 (see object.c
).
In Python 3, this was changed; now doing comparisons on things without a sensible natural ordering results in a TypeError
. From the 3.0 "what's new" updates:
Python 3.0 has simplified the rules for ordering comparisons:
The ordering comparison operators (
<
,<=
,>=
,>
) raise aTypeError
exception when the operands don’t have a meaningful natural ordering. Thus, expressions like:1 < ''
,0 > None
orlen <= len
are no longer valid, and e.g.None < None
raisesTypeError
instead of returningFalse
. A corollary is that sorting a heterogeneous list no longer makes sense – all the elements must be comparable to each other. Note that this does not apply to the==
and!=
operators: objects of different incomparable types always compare unequal to each other.
This upset some people since it was often handy to do things like sort a list that had some None
values in it, and have the None
values appear clustered together at the beginning or end. There was a thread on the mailing list about this a while back, but the ultimate point is that Python 3 tries to avoid making arbitrary decisions about ordering (which is what happened a lot in Python 2).
这篇关于一切都大于无吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!