Does Python need an 'xor' operator?

John Roth johnroth at ameritech.net
Sun Apr 14 07:56:06 EDT 2002


"Bengt Richter" <bokr at oz.net> wrote in message
news:a9aoqc$j94$0 at 216.39.172.122...
> On Sat, 13 Apr 2002 15:58:19 -0700, "Ken Peek"
<Ken.Peek at SpiritSongDesigns.comNOSPAM> wrote:
>
> >"Martin v. Loewis" <martin at v.loewis.de> wrote in message
> >news:m3ofgn1avt.fsf at mira.informatik.hu-berlin.de...
> >| "Ken Peek" <Ken.Peek at SpiritSongDesigns.comNOSPAM> writes:
> >|
> >| > I think an 'xor' operator is needed.  The 'xor'
> >| > operator means 'logical-exclusive-or', and
> >| > returns a 1 or 0.
> >|
> >| In Python 2.3, you will be able to write
> >|
> >|    bool(a) ^ bool(b)
> >|
> >| Seems pretty clear to me.
> >|
> >| Regards,
> >| Martin
> >
> >You didn't read my post well enough-- we can already _DO_
> >that with Python v2.2.x:
> >
> >(not a) ^ (not b)
> >
> >IMHO, neither the above, nor the future v2.3.x idiom:
> >
> >bool(a) ^ bool(b)
> >
> Hm, ^ should probably be != to return bools instead of integers?
>
> >is as clear as:
> >
> >a xor b
> >
> >...
> >
> >Everyone_is_entitled_to_have_my_opinion'ly yours,
> >
> Well, to tie in with existing spelled-out 'and' and 'or' operations,
> it should probably do something along those lines (i.e., evaluate to
> one or other of the objects, not one of the boolean values used in the
logic),
> but I can't think of something sensible.

If I remember my logic and the description of what the
short circuit 'and' and 'or' operators actually do, it would
have to return the second operand in all cases. That's
logically incorrect - the second operand doesn't
necessarally have the correct truth value. Nor does
the first operand. In fact, there is one case (both operands
false) where neither has the correct truth value!

So it's not possible to return one of the operands, we have
to return True or False. However, now the operation
is not consistent with 'and' and 'or,' which imposes an
issue with teaching the language.

John Roth
>
> Regards,
> Bengt Richter





More information about the Python-list mailing list