Is there a preferred method for doing a logical XOR in python?
For example, if I have two variables a and b, and I want to check that at least one exists but not both, I have two methods:
Method 1 (bitwise operator):
if bool(a) ^ bool(b):
do x
Method 2 (boolean operators):
if (not a and b) or (a and not b):
do x
Is there an inherent performance benefit to using either one? Method 2 seems more "pythonic" but Method 1 looks much cleaner to me. This related thread seems to indicate that it might depend on what variable types a
and b
are in the first place!
Any strong arguments either way?