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

Emanuel Barry vgr255 at live.ca
Sun Oct 18 00:59:23 CEST 2015




> To: python-ideas at python.org
> From: storchaka at gmail.com
> Date: Sun, 18 Oct 2015 01:49:42 +0300
> Subject: Re: [Python-ideas] Discussion about a 'xor' keyword?
> 
> 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).

I must admit I forgot about the short-circuit feature of the 'and' and 'or' operands while I was writing this. I agree it could be a function (I already use one for most cases), but was mostly suggesting the idea (rather than stating it as a hard necessity).
>Do you have additional arguments that weren't covered in the PEPs >discussing the addition of an XOR operator?>Emile
I must admit I did not see such PEP (I still don't, unless I'm not looking right). My bad in that case.
-Emanuel Barry
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151017/9baf2d65/attachment.html>


More information about the Python-ideas mailing list