Boolean identity == True vs is True

2020-02-02 06:31发布

It is standard convention to use if foo is None rather than if foo == None to test if a value is specifically None.

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.?

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':
    ...

Is there a case where using if foo is True would yield different results from if 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.


UPDATE: According to PEP 285 § Specification:

The values False and True will be singletons, like None.

6条回答
劫难
2楼-- · 2020-02-02 06:46

is there any reason to use if foo == True rather than if foo is True?"

>>> d = True
>>> d is True
True
>>> d = 1
>>> d is True
False
>>> d == True
True
>>> d = 2
>>> d == True
False

Note that bool is a subclass of int, and that True has the integer value 1. To answer your question, if you want to check that some variable "is exactly True", you have to use the identity operator is. But that's really not pythonic... May I ask what's your real use case - IOW : why do you want to make a difference between True, 1 or any 'truth' value ?

查看更多
Fickle 薄情
3楼-- · 2020-02-02 06:58

edit: regarding:

Is there a case where using if foo is True would yield different results from if foo == True?

there is a case, and it's this:

In [24]: 1 is True
Out[24]: False

In [25]: 1 == True
Out[25]: True

additionally, if you're looking to use a singleton as a sentinel value, you can just create an object:

sentinel_time = object()

def f(snth):
    if snth is sentinel_time:
        print 'got em!'
f(sentinel_time)

you don't want to use if var == True:, you really want if var:.

imagine you have a list. you don't care if a list is "True" or not, you just want to know whether or not it's empty. so...

l = ['snth']
if l:
    print l

check out this post for what evaluates to False: Evaluation of boolean expressions in Python

查看更多
祖国的老花朵
4楼-- · 2020-02-02 07:02

Here's a test that allows you to see the difference between the 3 forms of testing for True:

for test in ([], [1], 0, 1, 2):
    print repr(test), 'T' if test else 'F', 'T' if test == True else 'F', 'T' if test is True else 'F'

[] F F F
[1] T F F
0 F F F
1 T T F
2 T F F

As you can see there are cases where all of them deliver different results.

查看更多
看我几分像从前
5楼-- · 2020-02-02 07:04

Never use is True in combination with numpy (and derivatives such as pandas):

In[1]: import numpy as np
In[2]: a = np.array([1, 2]).any()
In[4]: a is True
Out[4]: False
In[5]: a == True
Out[5]: True

This was unexpected to me as:

In[3]: a
Out[3]: True

I guess the explanation is given by:

In[6]: type(a)
Out[6]: numpy.bool_
查看更多
等我变得足够好
6楼-- · 2020-02-02 07:08

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?

If you want to make sure that foo really is a boolean and of value True, use the is operator.

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.

As a rule of thumb, you should always use is with the built-in constants True, False and None.

Does this vary between implementations such as CPython (2.x and 3.x), Jython, PyPy, etc.?

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).

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.

查看更多
迷人小祖宗
7楼-- · 2020-02-02 07:10

Most of the time, you should not care about a detail like this. Either you already know that foo is a boolean (and you can thus use if foo), or you know that foo is something else (in which case there's no need to test). If you don't know the types of your variables, you may want to refactor your code.

But if you really need to be sure it is exactly True and nothing else, use is. Using == will give you 1 == True.

查看更多
登录 后发表回答