What works flawlessly, however, is:
In [1]: a = None In [2]: b = 0 In [3]: (a is None) ^ (b is None) Out[3]: True
In [4]: (a is None) != (b is None) Out[4]: True
In [5]: a or b Out[5]: 0
In [6]: b or a Out[6]: None # should be 0
In [10]: a if a is not None else b Out[10]: 0
What works flawlessly, however, is:
Alternatively, as suggested in another comment, you can use inequality as a replacement for XOR: Another error prone pattern is the following: Which can behave differently depending on the order of elements/values: Since b is zero, it doesn't count. Less error prone, but also more verbose is: