Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean identity == True vs is True

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.

like image 372
Uyghur Lives Matter Avatar asked Dec 03 '14 16:12

Uyghur Lives Matter


People also ask

What Boolean value is true?

Boolean values and operationsConstant true is 1 and constant false is 0. It is considered good practice, though, to write true and false in your program for boolean values rather than 1 and 0. The following table shows comparisons and boolean operations.

Is Boolean just true or False?

A Boolean variable has only two possible values: true or false. It is common to use Booleans with control statements to determine the flow of a program.

Is Boolean true or False or 1 or 0?

Boolean Variables and Data Type ( or lack thereof in C )Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.


2 Answers

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.

like image 189
Ferdinand Beyer Avatar answered Sep 21 '22 17:09

Ferdinand Beyer


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_ 
like image 35
kadee Avatar answered Sep 20 '22 17:09

kadee