问题描述
标准约定是使用 if foo is None
而不是 if foo == None
来测试一个值是否具体为 None
.
It is standard convention to use if foo is None
rather than if foo == None
to test if a value is specifically None
.
如果您想确定一个值是否完全是 True
(不仅仅是一个类似 true 的值),是否有任何理由使用 if foo == True
而不是比if foo 为True
?这在 CPython(2.x 和 3.x)、Jython、PyPy 等实现之间是否有所不同?
If you want to determine whether a value is exactly True
(not just a true-like value), is there any reason to use if foo == True
rather than if foo is True
? Does this vary between implementations such as CPython (2.x and 3.x), Jython, PyPy, etc.?
示例:说 True
用作您想要与值 'bar'
或任何其他类似 true 的值区分开来的单例值:
Example: say True
is used as a singleton value that you want to differentiate from the value 'bar'
, or any other true-like value:
if foo is True: # vs foo == True
...
elif foo == 'bar':
...
是否存在使用 if foo is True
会产生与 if foo == True
不同的结果的情况?
Is there a case where using if foo is True
would yield different results from if foo == True
?
注意:我知道 Python 布尔值 - if x:, vs if x == True, vs if x is True一个>.但是,它只处理 if foo
、if foo == True
或 if foo is True
通常是否应该用于确定 foo
有一个类似于 true 的值.
NOTE: I am aware of Python booleans - if x:, vs if x == True, vs if x is True. However, it only addresses whether if foo
, if foo == True
, or if foo is True
should generally be used to determine whether foo
has a true-like value.
更新:根据 PEP 285 § 规范:
UPDATE: According to PEP 285 § Specification:
值 False 和 True 将是单例,例如 None.
The values False and True will be singletons, like None.
推荐答案
如果您想确定一个值是否完全为 True(而不仅仅是类似 true 的值),是否有任何理由使用 if foo == True 而不是 if foo is True?
If you want to determine whether a value is exactly True (not just a true-like value), is there any reason to use if foo == True rather than if foo is True?
如果您想确保 foo
确实是一个布尔值且值为 True
,请使用 is
运算符.
If you want to make sure that foo
really is a boolean and of value True
, use the is
operator.
否则,如果 foo
的类型实现了它自己的 __eq__()
在与 True
比较时返回一个真值,你可能会导致意想不到的结果.
Otherwise, if the type of foo
implements its own __eq__()
that returns a true-ish value when comparing to True
, you might end up with an unexpected result.
根据经验,您应该始终将 is
与内置常量 True
、False
和 None<一起使用/代码>.
As a rule of thumb, you should always use is
with the built-in constants True
, False
and None
.
这在 CPython(2.x 和 3.x)、Jython、PyPy 等实现之间是否有所不同?
Does this vary between implementations such as CPython (2.x and 3.x), Jython, PyPy, etc.?
理论上,is
会比 ==
更快,因为后者必须遵守类型的自定义 __eq__
实现,而 is
可以直接比较对象的身份(例如,内存地址).
In theory, is
will be faster than ==
since the latter must honor types' custom __eq__
implementations, while is
can directly compare object identities (e.g., memory addresses).
我不知道各种 Python 实现的源代码,但我认为它们中的大多数可以通过使用一些内部标志来优化它,以确保魔法方法的存在,所以我怀疑你不会注意到实践中的速度差异.
I don't know the source code of the various Python implementations by heart, but I assume that most of them can optimize that by using some internal flags for the existence of magic methods, so I suspect that you won't notice the speed difference in practice.
这篇关于布尔标识 == True vs 是 True的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!