Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to replace '==' with 'is' to compare Boolean-values

Tags:

python

I did several Boolean Comparisons:

>>> (True or False) is True True >>> (True or False) == True True 

It sounds like == and is are interchangeable for Boolean-values.

Sometimes it's more clear to use is

I want to know that:

Are True and False pre-allocated in python?

Is bool(var) always return the same True(or False) with the pre-allocated True(or False)?

Is it safe to replace == with is to compare Boolean-values?


It's not about Best-Practice.

I just want to know the Truth.

like image 238
kev Avatar asked Jan 04 '11 06:01

kev


People also ask

Can we compare two boolean values?

We use the compare() method of the BooleanUtils class to compare two boolean values. The method takes two values and returns true if both the values are the same. Otherwise, it returns false .

Can you use == to compare boolean?

The . equals() methods seems to be roughly 4 times slower than == . Thus, it is safe to say that . equals() hinders performance and that == is better to use in most cases to compare Boolean .

Which operator is used to compare two boolean?

The first, = is the assignment operator, which will set one value equal to another. The second, == is a comparison operator which will evaluate whether two values are equal.


2 Answers

You probably shouldn't ever need to compare booleans. If you are doing something like:

if some_bool == True:   ... 

...just change it to:

if some_bool:   ... 

No is or == needed.

As commenters have pointed out, there are valid reasons to compare booleans. If both booleans are unknown and you want to know if one is equal to the other, you should use == or != rather than is or is not (the reason is explained below). Note that this is logically equivalent to xnor and xor respectively, which don't exist as logical operators in Python.

Internally, there should only ever be two boolean literal objects (see also the C API), and bool(x) is True should be True if bool(x) == True for any Python program. Two caveats:

  • This does not mean that x is True if x == True, however (eg. x = 1).
  • This is true for the usual implementation of Python (CPython) but might not be true in other implementations. Hence == is a more reliable comparison.
like image 50
detly Avatar answered Sep 22 '22 18:09

detly


Watch out for what else you may be comparing.

>>> 1 == True True >>> 1 is True False 

True and False will have stable object ids for their lifetime in your python instance.

>>> id(True) 4296106928 >>> id(True) 4296106928 

is compares the id of an object

EDIT: adding or

Since OP is using or in question it may be worth pointing this out.

or that evaluates True: returns the first 'True' object.

>>> 1 or True 1 >>> 'a' or True 'a' >>> True or 1 True 

or that evaluates False: returns the last 'False' object

>>> False or '' '' >>> '' or False False 

and that evaluates to True: returns the last 'True' object

>>> True and 1 1 >>> 1 and True True 

and that evaluates to False: returns the first 'False' object

>>> '' and False '' >>> False and '' False 

This is an important python idiom and it allows concise and compact code for dealing with boolean logic over regular python objects.

>>> bool([]) False >>> bool([0]) True >>> bool({}) False >>> bool({False: False}) True >>> bool(0) False >>> bool(-1) True >>> bool('False') True >>> bool('') False 

Basically 'empty' objects are False, 'non empty' are True.

Combining this with @detly's and the other answers should provide some insight into how to use if and bools in python.

like image 29
kevpie Avatar answered Sep 18 '22 18:09

kevpie