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

Emanuel Barry vgr255 at live.ca
Sun Oct 18 00:17:47 CEST 2015


You've probably read this title with skepticism. I know I would.
As the title says. I am curious as to engage in the discussion, and see what everyone's thoughts are on it. I know this has been discussed in the past several years ago ( https://mail.python.org/pipermail/python-3000/2007-December/011560.html ) and on various websites already. I've found myself writing code that would be simpler with a xor keyword or similar syntax, and this is why I'm suggesting this.
Here are my thoughts:
Since 'a or b' and 'a and b' return either a or b, what should 'a xor b' return? I have a few ideas, and no particular preference:
To be consistent with other binary operators, 'xor' should return one of its operands, IMO. So 'a xor b' should be a if a is True and b is False, and b if b is True and a is False. That much makes sense so far (to me). What I'm a bit less sure about is what to return in the case that both are True, or both False.
We already have a per-type definition of xor, and if we want to have only the boolean xor, we can easily do 'bool(a) ^ bool(b)' and so I feel this discussion should disregard the possibility of returning only a boolean value; we can already do that in a way that a keyword wouldn't help.
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.
I'm expecting there'll be some sort of BDFL pronouncement at some point, but I still want to have the discussion. If it gets rejected, I would at least be happy if some discussion took place to talk about it.
-Emanuel Barry 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151017/73cc5bf0/attachment.html>


More information about the Python-ideas mailing list