使用算术运算符将 None 与内置类型进行比较?

发布于 2024-12-28 11:10:38 字数 417 浏览 4 评论 0原文

Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> None > 0
False
>>> None == 0
False
>>> None < 0
True
  • 是否使用为内置类型(在本例中为整数)定义良好的算术运算符来比较 None
  • 前两个和第三个比较部分之间的差异是语言规范的一部分(Python 的规范 - 你一定是在开玩笑:))还是 CPython 的实现细节?
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> None > 0
False
>>> None == 0
False
>>> None < 0
True
  • Is comparing None using arithmetic operators well defined for built-in types (integers in this case)?
  • Is the difference between the first two and the third comparison part of language specification (Python's specification - you must be kidding :)) or is it CPython's implementation detail?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白馒头 2025-01-04 11:10:38

真正可以与 None 进行比较的唯一有意义的比较是 if obj is None: (或 if obj is not None:)。

不同类型之间的比较已从 Python 3 中删除,原因很充分——它们是错误的常见来源并导致混乱。例如

>>> "3" < 4
False

,在 Python 3 中,当比较不同类型的值(例如 strint 或任何内容与 None)时,您会收到 TypeError 错误

>>> None < 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: NoneType() < int()

(我的意思是“比较”,试图确定两个值中哪一个更大/更小。比较相等性是可以的 - 如果两个对象属于不同类型,它总是返回 False。

)尚未在文档中找到这方面的参考资料,但在 学习 Python,第四版 中,Mark Lutz 写道第 204 页:

[...] 不同类型对象的比较(例如,字符串和
list) work — 该语言定义了不同类型之间的固定顺序
类型,即使不美观,也是确定性的。那是,
排序基于所涉及类型的名称:所有整数
小于所有字符串,例如,因为 "int" 小于
“str”

The only meaningful comparison you can really use with None is if obj is None: (or if obj is not None:).

Comparison between different types has been removed from Python 3 for good reasons - they were a common source of errors and lead to confusion. For example

>>> "3" < 4
False

In Python 3, you get a TypeError when comparing values of different types like str vs. int or anything vs. None.

>>> None < 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: NoneType() < int()

(I mean "comparing" in the sense of trying to determine which of two values is larger/smaller. Comparison for equality is OK - it will always return False if two object are of different types.)

I haven't found a reference in the docs for this, but in Learning Python, 4th edition, Mark Lutz writes on page 204:

[...] Comparisons of differently typed objects (e.g., a string and a
list) work — the language defines a fixed ordering among different
types, which is deterministic, if not aesthetically pleasing. That is,
the ordering is based on the names of the types involved: all integers
are less than all strings, for example, because "int" is less than
"str".

注定孤独终老 2025-01-04 11:10:38

来自 http://bytes.com/topic/python 的一些有趣的引用/answers/801701-why-none-0-a

在早期的 Python 中,决定比较 any
两个对象是合法的并且会返回一致的结果。所以对象
不同类型的将根据其顺序进行比较
类型(依赖于实现,未指定,但一致
ordering),并且相同类型的对象将根据
对该类型有意义的规则。

其他实现有权比较整数和 None
有所不同,但在具体实现上,结果不会
改变。

Python 3 将在此类比较中引发异常。

该问题属于典型问题; Python 最初并没有
布尔类型,改造导致了奇怪的语义。 C 有
同样的问题。

Some interesting quotes from http://bytes.com/topic/python/answers/801701-why-none-0-a

In early Python, the decision was made that the comparison of any
two objects was legal and would return a consistent result. So objects
of different types will compare according to an ordering on their
types (an implementation dependent, unspecified, but consistent
ordering), and objects of the same type will be compared according to
rules that make sense for that type.

Other implementations have the right to compare an integer and None
differently, but on a specific implementation, the result will not
change.

Python 3 will raise an exception on such comparisons.

and

The problem is the typical one; Python did not originally have a
Boolean type, and the retrofit resulted in weird semantics. C has the
same issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文