gpt4 book ai didi

python - 一切都大于无吗?

转载 作者:IT老高 更新时间:2023-10-28 21:41:47 25 4
gpt4 key购买 nike

除了 None 之外,是否还有 Python 内置数据类型:

>>> not foo > None
True

foo 是那种类型的值? Python 3 怎么样?

最佳答案

None总是小于 Python 2 中的任何数据类型(参见 object.c )。

在 Python 3 中,这已更改;现在对没有合理自然排序的事物进行比较会导致 TypeError .来自 3.0 "what's new" updates :

Python 3.0 has simplified the rules for ordering comparisons:

The ordering comparison operators (<, <=, >=, >) raise a TypeError exception when the operands don’t have a meaningful natural ordering. Thus, expressions like: 1 < '', 0 > None or len <= len are no longer valid, and e.g. None < None raises TypeError instead of returning False. 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.

这让一些人感到不安,因为对包含一些 None 的列表进行排序之类的操作通常很方便。值,并具有 None值在开始或结束时聚集在一起。 There was a thread on the mailing list about this 不久前,但最终的一点是 Python 3 试图避免对排序做出任意决定(这在 Python 2 中经常发生)。

关于python - 一切都大于无吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2214194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com