Does Python need an 'xor' operator?

David Eppstein eppstein at ics.uci.edu
Sun Apr 14 18:50:39 EDT 2002


In article <3cba0068 at news.mhogaming.com>,
 "Ken Peek" <Ken.Peek at SpiritSongDesigns.comNOSPAM> wrote:

> # give me either a or b, but only if one
> # of them is true, else give me false:
> c = a xor b
> 
> # the above would be equivalent to:
> def xor(a,b):
>     if   a and not b: return a
>     elif b and not a: return b
>     else:             return false

Or shorter

    def xor(a,b):
        return (not b and a) or (not a and b) or False

-- 
David Eppstein       UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/



More information about the Python-list mailing list