编程语言
首页 > 编程语言> > python – 一切都比无?

python – 一切都比无?

作者:互联网

参见英文答案 > A number smaller than negative infinity in python?                                    1个
除了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-datamodel,python,python-3-x
来源: https://codeday.me/bug/20190917/1810082.html