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
?
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: