boolean OR gotcha

1x7y2z9 1x7y2z9 at gmail.com
Tue Aug 4 22:11:18 CEST 2009


>>> 0 or None is None
True
>>> None or 0 is None
False
>>> None or 0 is 0
True

Yes, this is explained in the docs:
The expression x or y first evaluates x; if x is true, its value is
returned; otherwise, y is evaluated and the resulting value is
returned.

Another one (also explainable):
>>> 0 or None == None or 0
True
# Above is same as (operator precedence):
>>> 0 or (None == None) or 0
True
# Here is something different:
>>> (0 or None) == (None or 0)
False



More information about the Python-list mailing list