-2
not None is False -> True

so I'd expect

None is True -> True

but this is not the case, is there some inner working of the None class to be like this?

I actually only ever used if xxx is not None to check for boolean and never thought that if I used if xxx is None, which will evaluate to False and my logic will be wrong.

Is this behaviour expected?

ilovewt
  • 911
  • 2
  • 10
  • 18
  • This is the same as asking why does `if x` evaluate to true and `if not x` evaluate to false for the same value of `x`. That's just how boolean logic works. – Code-Apprentice May 22 '22 at 03:40
  • `None is False` is false, so applying `not` to that makes it true. Were you expecting `not None` to be evaluated first? – John Gordon May 22 '22 at 03:41

2 Answers2

2

So if I understand correctly, you are doing

>>> not None is False
True
>>> None is True
False

And you are wondering why the second one outputs False.

This is because not None is False is the same as not (None is False) which is not False which is True. On the other hand None is True just evaluates immediately to False.

On a side note, you seem to think that not None is False would evaluate as (not None) is False. However, if this were the case, the result would be False by this evaulation:

(not None) is False -> True is False -> False

So this can't be the correct interpretation because this is not the result we get.

The key here is to evaluate one piece of an expression at a time. If you are familiar with order of operations (PEMDAS) from math, python has similar rules for all operators. Understanding this order of operations is critical for evaluating any expression in python.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
-1

None or empty iterable or False all will not be executed by if statement.

so

>>> not None is False
True
>>> None is True
False
>>> arr = list()
>>> if arr:
...     print ("*.*")
... 
>>> arr.append(1)
>>> if arr:
...     print ("*.*")
... 
*.*
>>> 
Udesh
  • 2,415
  • 2
  • 22
  • 32