missing 'xor' Boolean operator

Chris Rebert clp2 at rebertia.com
Wed Jul 15 19:02:13 EDT 2009


On Wed, Jul 15, 2009 at 3:57 PM, Wayne Brehaut<wbrehaut at mcsnet.ca> wrote:
> On 15 Jul 2009 09:11:44 GMT, Steven D'Aprano
> <steven at REMOVE.THIS.cybersource.com.au> wrote:
>
>>On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote:
>>
>>> Current Boolean operators are 'and', 'or', and 'not'.  It would be nice
>>> to have an 'xor' operator as well.
>>
>>I've often wished there was too, for the sake of completeness and
>>aesthetics, I'd love to be able to write:
>>
>>a xor b
>>
>>instead of defining a function xor(a, b).
>>
>>Unfortunately, outside of boolean algebra and simulating electrical
>>circuits, I can't think of any use-cases for an xor operator. Do you have
>>any?
>
> Since xor in set theory is the symmetric difference,  perhaps we'd
> like to know all items in exactly one of two word lists or
> dictionaries, or anything else we could easily set-ize:
>
>>>> cheese = set(['cheddar', 'limburger', 'stilton'])
>>>> stinky = set(['skunk', 'limburger', 'stilton', 'polecat', 'doggy-doo', 'civet'])
>>>> nasty = set(['doggy-doo', 'polecat', 'limburger', 'Perl'])
>>>> cheese & stinky # stinky cheese
> set(['limburger', 'stilton'])
>>>> cheese ^ stinky # either cheese or stinky but not both
> set(['doggy-doo', 'civet', 'polecat', 'skunk', 'cheddar'])
>>>> cheese ^ stinky ^ nasty # in an odd number of these sets (1 or 3)
> set(['civet', 'cheddar', 'Perl', 'limburger', 'skunk'])
>
> Who hasn't needed that occasionally?

This discussion is about adding a *logical* operator for use in
boolean expressions. We obviously already have ^ for non-boolean use.

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-list mailing list