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

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Oct 18 01:28:01 CEST 2015


On 17/10/2015 23:50, Emile van Sebille wrote:
> Do you have additional arguments that weren't covered in the PEPs
> discussing the addition of an XOR operator?
>
> Emile
>

The only one I can find that refers specifically to xor is PEP 225 -- 
Elementwise/Objectwise Operators[1].  Which others have I missed?

[1] https://www.python.org/dev/peps/pep-0225/

>
> On 10/17/2015 3:17 PM, Emanuel Barry 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.
>>
>> 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

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Python-ideas mailing list