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

Chris Rebert pyideas at rebertia.com
Sun Oct 18 02:40:54 CEST 2015


On Sat, Oct 17, 2015 at 3:17 PM, Emanuel Barry <vgr255 at live.ca> wrote:
> 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.

There were a couple times in the past when I found myself wanting a
"xor" operator for clarity/aesthetics. In the most recent case, it was
to express a validity condition for rows in an SQL DB table which was
acting like a tagged union, so only 1 of a subset of the columns in a
given row could be non-NULL. That case would have been served just
fine by a simple version of xor that always gave a bool result.

However, it ultimately turned out that what I really wanted was an
"exactly 1 of these N subconditions must be true" operation, which
doesn't correspond to conventional definitions of XOR (they instead
calculate a parity bit; see http://mathworld.wolfram.com/XOR.html ).
So xor would only truly have been useful in cases when N
just-so-happens to be 2, and therefore I no longer hunger for a "xor"
keyword.

I would be interested to hear what your concrete use cases for xor are.

Cheers,
Chris
--
http://chrisrebert.com


More information about the Python-ideas mailing list