[Python-ideas] Discussion about a 'xor' keyword?

Serhiy Storchaka storchaka at gmail.com
Sun Oct 18 00:49:42 CEST 2015


On 18.10.15 01:17, Emanuel Barry wrote:
> My thoughts on this would be to return either a or b if only one of them
> is True in a boolean context, else return None. None is a fairly common
> name already, and we could use it to say that the exclusive-or check
> returned no value (just like a function call). I don't think adding a
> new builtin name would be the way to go - adding a new keyword is
> already a big breaking change. If one of the two values is None
> already... well then I guess that's something to be fixed in the code?
> That's one of the things that should probably be discussed, should the
> people on this list be favorable to the idea.

'and' and 'or' are short-circuit operators. But 'xor' needs to know 
values of both operands. Therefore no need in the xor operator, it can 
be a function.

     def xor(a, b):
         if a:
             if b:
                 return None
             else:
                 return a
         else:
             if b:
                 return b
             else:
                 return None

But I suppose that in most cases boolean result is enough, and it can be 
written as bool(a) != bool(b).




More information about the Python-ideas mailing list