Search code examples
pythontypesnonetype

Comparing None to several variables in Python3


Is there anyone that can tell my why I get this type of return value?

>>> a = 7
>>> b = None
>>> bool(a and b)
False
>>> bool((a is None) and (b is None))
False
>>> bool((a and b) is None) # (a and b) eq False
True
>>> bool(False is None)
False

I just can't understand why this is happening.


Solution

  • To explain

    >>> 7 and None
    None
    >>> bool(None)
    False
    

    So to answer:

    • a and b gives None and not False as you put in comment.
    • bool(a and b) gives False

    So then when you replace a and b by its real value:

    • you get bool(None is None) which is True.

    I believe you had bool(bool(a and b) is None) in mind, which would give False