24

Which is better? (and why?)

if somevalue == False:

or

if somevalue is False:

Does your answer change if somevalue is a string?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
patchwork
  • 1,161
  • 3
  • 8
  • 23
  • 5
    I would write `if not somevalue:` although for non boolean types it will behave differently: `if not "":` will run the code but `if "" == False:` will not. – rodrigo May 08 '16 at 19:08
  • You generally don't explicitly compare objects to `False`/`True` in Python. Just do `if somevalue:` or `if not somevalue:` – Eli Korvigo May 08 '16 at 19:08

2 Answers2

30

It rather depends on what somevalue can be: if somevalue could be anything you could check that it's a boolean and not:

if isinstance(somevalue, bool) and not somevalue

this doesn't rely on False being a singleton. If it always is a singleton you can also do:

if somevalue is False

But PEP8 of Python states you shouldn't care if it about the class and just use:

if not somevalue

this will evaluate if somevalue is "falsy". See Python documentation on Truth value testing.

PEP8 states:

Don't compare boolean values to True or False using == .

and gives these examples:

Yes:   if greeting:
No:    if greeting == True:
Worse: if greeting is True:

which translates in your case to:

Yes:   if not greeting:
No:    if greeting == False:
Worse: if greeting is False:

Keep in mind that each string is considered "truthy" except the empty string ''.

MSeifert
  • 145,886
  • 38
  • 333
  • 352
3

is is checking whether two objects are literally the same object. == is checking for equality.

If you want to check whether something is false, e.g. zero, opposite of True, do if something == False.

As mentioned in the comments, you can certainly do if not something, it may improve readability quite a bit, but it's the same as if bool(something) == False.

I prefer equality, because, to my mind, True and False should behave just like 1 and 0 in terms of being true or false. For example ("a" == "a") == False evaluates to False just like ("a" == "a") == 0 does.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • 1
    mm, `if not something`? – vaultah May 08 '16 at 19:07
  • @vaultah, yes, but if the question's stated like: choose either `==` either `is`, it's better to use `==` – ForceBru May 08 '16 at 19:08
  • 1
    *but it's the same as `if something == False`*, no. It's the same as `if bool(something) == False`. – vaultah May 08 '16 at 19:11
  • 1
    @vaultah, _It's the same as `if bool(something) == False`_, true, but it's just a verbose way to say `if something == False` – ForceBru May 08 '16 at 19:13
  • `'' == False` is False, but `bool('') == False` is True. – vaultah May 08 '16 at 19:14
  • @vaultah, well, you're right – ForceBru May 08 '16 at 19:16
  • Using is with False/True relies on an implementation detail of CPython. None is guaranteed to be a singleton. True and False are not. So e.g. pypy which doesn't need the small-number-cache CPython has could break your code, or needs to add extra treatment for is True/False, slowing things down. Edit: misattributed vaultah – deets May 08 '16 at 19:23
  • 2
    @deets: Are you sure about that? PEP 285 says: "The values False and True will be singletons, like None." – Mark Dickinson May 08 '16 at 19:27
  • @MarkDickinson I was, but I stand corrected. Removing my answer. – deets May 08 '16 at 19:30