python中小于负无穷大的数字? [英] A number smaller than negative infinity in python?
问题描述
这在 python2 中是可能的:
This is possible in python2:
None < float('-inf')
此外,它总是返回
True
然而,在python3上,这会抛出
However, on python3, this throws
TypeError: unorderable types: NoneType() < int()
为什么 None
与 python2 的整数/浮点数相比?None
在 python2 中可订购有什么好处或应用吗?
Why is None
comparable to integers/floats with python2? Are there any benefits or applications to None
being orderable in python2?
推荐答案
首先,Python 2 允许比较所有类型的混合类型.这个问题最终在 Python 3 中修复.
First of all Python 2 allowed comparing all types of mixed types. This wart was fixed in Python 3 eventually.
CPython 实现细节:不同类型的对象,除了数字按其类型名称排序;相同类型的对象不支持正确比较的按地址排序.
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.
对于 None
一个快速决定由 Guido 和 Tim Peters 制作,并在 Python 中生成了 commit2.1(强调我的):
For None
a quick decision was made by Guido and Tim Peters and it resulted in this commit in Python 2.1(emphasis mine):
修复的一部分是删除比较对象的某些情况"通过比较类型名称字符串来不同类型".Guido & 我是当时两人都在办公室,一个人对另一个人说:那怎么办?无
?通过比较字符串 'None'
将其与其他类型进行比较没有多大意义." 是的,好吧……将 None
更改为 - by默认 - 比较其他类型的小于"对象?" "没看到为什么不 - 确定." 好的,完成!
Part of fixing that was removing some cases of "compare objects of different types by comparing the type name strings". Guido & I were both in the office at the time, and one said to the other: "what about
None
? Comparing that to other types via comparing the string'None'
doesn't make much sense." "Ya, OK ... how about changingNone
to - by default - comparing 'less than' objects of other types?" "Don't see why not - sure." "OK, done!
思考时间不超过 2 分钟.没有意图迎合这里的任何实际用例 - 唯一的目的是选择一些没有相当那么糟糕的任意但一致的规则假装 None 是字符串 "None" ;-)
No more than 2 minutes of thought went into it. There was no intent to cater to any real use case here - the only intent was to pick some arbitrary-but-consistent rule that didn't suck quite as badly as pretending None was the string "None" ;-)
这篇关于python中小于负无穷大的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!