为什么不是“是”?用于代替“ ==”的比较对于原始类型? [英] Why isn't "is" comparison used in place of "==" for primitive types?

查看:104
本文介绍了为什么不是“是”?用于代替“ ==”的比较对于原始类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Pytest进行Python格式化时,它抱怨这样做:

When I'm using Pytest for Python formatting, it complains about doing something like:

>>> assert some_function_ret_val() == True
E712 comparison to True should be 'if cond is True:' or 'if cond:'

并希望:

assert some_function_ret_val() is True

我知道只能有一个True / False / None副本,但我认为所有原语都是不可变的类型。

I know there can only be one copy of True/False/None, but I thought all primitives are immutable types.

在什么情况下原始类型的 ==和 is比较会有所不同?

Under what circumstances would "==" and "is" comparison be different for primitive types??

否则,为什么会有 ==成为比较任务的常态吗?

Otherwise, why has "==" become the norm in comparison tasks?

我发现了这个stackoverflow帖子,它谈到了与非原始类型的比较,但是我似乎找不到原因因为为什么是比较对于原始类型可能是危险的。
与布尔值numpy数组VS PEP8 E712的比较

I found this stackoverflow post that talks about comparison with non-primitive types, but I can't seem to find a reason for why "is" comparison might be dangerous with primitive types. Comparison with boolean numpy arrays VS PEP8 E712

如果只是约定俗成,我认为是比 ==更易读,但我觉得可能存在一些疯狂的情况,也许

If it's just convention, I would think that "is" is more legible than "==", but I feel like there could be some crazy edge cases where maybe there are more than one copy of a primitive type.

推荐答案

Python没有原始类型。 Python中的所有内容都是一个对象。

Python does not have primitive types. Everything in Python is an object.

通常,您唯一应该使用的地方是在语言保证的单例上,例如 True False None ,或者说出于调试目的,您实际上要检查对象身份。

Generally, the only place you should use is are on language-guaranteed singletons, like True, False, and None or, say for debugging purposes, you actually want to check object identity.

在其他每种情况下,您将依赖于实现细节和特定于实现的情况如果使用 is 表示相等,则表示优化(例如,窥视孔优化器和字符串插入)。相等运算符为 == ,在这种情况下应使用该运算符。虽然通常,Python解释器会优化不可变的类型,但当您指的是平等时,您仍不应依赖身份,因为多数情况下这不是语言保证

In every other case, you will be relying on implementation details and implementation-specific optimizations if you use is to mean equality (e.g. the peep-hole optimizer and string interning). The equality operator is == and should be used in those cases. While often, the Python interpreter will optimize immutable types, you should still not rely on identity when you mean equality, because mostly that is not a language guarantee.

例如,在CPython 3.7上,您可以安全地 尝试使用 is 来比较 small整数,因为它们已被缓存,这是实施细节,应依赖。在Python 3.9中或任何时候都可以自由更改。另外,请参阅@ user2357112的评论,它对于缓存的小整数甚至不一定安全!重申一下:这不是语言保证,而是实现方式的副作用。

As an example, while on CPython 3.7, you can "safely" be tempted choose to use is to compare small integers because they are cached, this is an implementation detail that should not be relied upon. This is free to change in Python 3.9 or whenever. Also, see the comment by @user2357112 about how it isn't even necessarily safe for the small integers that are cached! To reiterate: it is not a language guarantee - it is a side-effect of how it was implemented.

此外,它仅适用于小整数,[ -5,256]这样:

And also, again, it only applies to small integers, [-5, 256] so:

>>> def add(a, b): return a + b
...
>>> 16 is add(8, 8)
True
>>> 1000 is add(500, 500)
False

请注意,我将实际加作为一个函数,解释器经常优化不可变的文字和算术表达式:

Note, I put the actual addition in a function, the interpreter frequently optimizes immutable literals and arithmetic expressions:

>>> 1000 is (500 + 500)
True

但是现在应该很明显为什么不能

But it should be obvious now why you cannot rely on that.

另一个适合使用 is 进行平等比较的示例是比较枚举类型,这些类型保证为单例:

Another example where it is appropriate to use is for "equalityish" comparisons is to compare enum types, which are guaranteed singletons:

import enum
class Color(enum.Enum):
    RED = 1
    BLUE = 2

RED = Color.RED
BLUE = Color.BLUE

print(Color(1) is RED)

这篇关于为什么不是“是”?用于代替“ ==”的比较对于原始类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆