98

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.

kev
  • 155,172
  • 47
  • 273
  • 272

7 Answers7

94

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.
Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
detly
  • 29,332
  • 18
  • 93
  • 152
  • @detly, none of the answers really did. Even kevpie's didn't address whether `True` and `False` were the only instances, or really have any documentation. Yours is probably the most practical. – Matthew Flaschen Jan 04 '11 at 06:38
  • 7
    @Matthew @detly: Sorry, on reflection I should not have down-voted but merely left a comment. It seems most people imagine "True == True" as a stand-in for " == True", and in this case we should just write "" as suggested. But this misses the (in my opinion obvious) possibility that "True == True" is actually a stand-in for " == ", which is a perfectly valid thing to write. I can't undo my downvote anymore. Nevertheless I still feel the original (unedited) answer was bad. – Mitch Schwartz Jan 04 '11 at 06:47
  • @Mitch Schwartz - okay, that makes things clearer. By the way, you should be able to reverse a downvote shortly after an answer is edited, but if not, no big deal. – detly Jan 04 '11 at 06:52
  • @detly: Oh, you're right. I forgot that editing affects the 5 minute window. Changed to an upvote because of the edit and because I feel bad about how I handled it originally. – Mitch Schwartz Jan 04 '11 at 07:12
  • 3
    There are cases where you need to compare booleans, for example when the boolean values are unknown at the compare-time. This need can easily emerge when you interpret the booleans more like bits of information rather than truth-values and is exactly as valid as the xor operator `^`. – user1747134 Dec 13 '16 at 09:06
  • @user1747134 Yep, using `!=` for `xor` is a very useful trick! – detly Dec 13 '16 at 09:41
  • @user1747134 I think I've addressed your feedback. – detly Dec 14 '16 at 08:09
  • @detly, I use ` is True` & ` is False` sometimes, as low-cost input validation techniques (in addition to *actual* input-validation functions.) You said "probably shouldn't ever" in your answer, can you see anything wrong with this usecase^? – Rob Truxal Jun 22 '17 at 20:44
  • 5
    One case where you may want to compare booleans is with Django's `BooleanField`, which can have a value of `None`. For a model called `my_model` with field `my_bool`, the statement `if not my_model.my_bool` would execute whether the field value was `False` or `None`, which wouldn't happen if the statement read `if my_model.my_bool == False`. Kind of obvious, and not relevant to all scenarios, but worth considering. – alstr Oct 31 '18 at 13:33
  • Use `==` if you compare two variables that can have any "truthy" or "falsy" value. Use `is` if you compare two strictly boolean variables. – Jeyekomon Sep 07 '21 at 15:26
57

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.

kevpie
  • 25,206
  • 2
  • 24
  • 28
  • 5
    right. `bool(1) ==> True` so `bool(1) is True ==> True`. **is** should be used when you must have the same instance of an object. – kevpie Jan 04 '11 at 06:26
6

Yes. There are guaranteed to be exactly two bools, True and False:

Class bool cannot be subclassed further. Its only instances are False and True.

That means if you know both operands are bool, == and is are equivalent. However, as detly notes, there's usually no reason to use either in this case.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
4

It seems that all answers deal with True and False as defined after an interpreter startup. Before booleans became part of Python they were often defined as part of a program. Even now (Python 2.6.6) they are only names that can be pointed to different objects:

>>> True = 1
>>> (2 > 1)
True
>>> (2 > 1) == True
True
>>> (2 > 1) is True
False

If you have to deal with older software, be aware of that.

0

== and is are both comparison operators, which would return a boolean value - True or False. True has a numeric value of 1 and False has a numeric value of 0.

The operator == compare the values of two objects and objects compared are most often are the same types (int vs int, float vs float), If you compare objects of different types, then they are unequal. The operator is tests for object identity, 'x is y' is true if both x and y have the same id. That is, they are same objects.

So, when you are comparing if you comparing the return values of same type, use == and if you are comparing if two objects are same (be it boolean or anything else), you can use is.

42 is 42 is True and is same as 42 == 42.

Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
  • 5
    Your last assertion is false. 42 is 42 is True and is **NOT** same as 42 == 42. `is` is for identity comparison and it just happens that sometimes two numbers have the same `id` (for optimization reasons). Test this: `256 is (255+1) == True` and `256 is (255+1) == False` (that limit may differ between python versions). – ivanalejandro0 Jan 22 '14 at 19:10
  • Thanks for your comment. But confused a bit by your last expression (256 is (255+1)) == True ; returns true – Senthil Kumaran Jan 22 '14 at 20:57
  • Well, I think that in your case may apply the "(that limit may differ between python versions)". If you test with bigger numbers you will get `False` at some point, the identity equality among numbers is related to a python optimization... Here: http://stackoverflow.com/a/306353/687989 is an explanation about that issue. – ivanalejandro0 Feb 03 '14 at 01:07
0

Another reason to compare values using == is that both None and False are “falsy” values. And sometimes it’s useful to use None to mark a value as “not defined” or “no value” while considering True and False values to work with:

def some_function(val = None):
    """This function does an awesome thing."""
    if val is None:
        # Values was not defined.
    elif val == False:
        # User passed boolean value.
    elif val == True:
        # User passed boolean value.
    else:
       # Quack quack.

Somewhat related question: Python != operation vs “is not”.

Jens
  • 8,423
  • 9
  • 58
  • 78