布尔标识 == True vs 是 True

2023-09-01Python开发问题
6

本文介绍了布尔标识 == True vs 是 True的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

标准约定是使用 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 fooif foo == Trueif 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 与内置常量 TrueFalseNone<一起使用/代码>.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11