I have two python objects a
and b
.
What is the best/most-efficient/most-pythonic way to check if exactly one of these objects is None
?
Use Python's ^
(XOR) operator.
(a is None) ^ (b is None)
What you effectively want is the XOR or Exclusive OR function on whether those two objects are None. The following should work for you:
(a is None) ^ (b is None)
A more exhaustive answer on how to get XOR on objects in Python can be found here:
How do you get the logical xor of two variables in Python?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With