missing 'xor' Boolean operator

Mark Dickinson dickinsm at gmail.com
Mon Jul 27 05:12:16 EDT 2009

On Jul 27, 1:53 am, "Delaney, Timothy (Tim)" <tdela... at avaya.com>
> Mark Dickinson wrote:
> >> Since the 'and' and 'or' already return objects (and objects
> >> evaluate to true or false), then 'xor' should behave likewise, IMO.
> >> I expect that would be the case if it were ever added to the
> >> language.
> > I'm not so sure.  Did you ever wonder why the any() and all()
> > functions introduced in 2.5 return a boolean rather than returning
> > one of their arguments?  (I did, and I'm still not sure what the
> > answer is.)
> Consider the case of any() and all() operating on an empty iterable.
> What type should they return?
> It is impossible in the case of any() and all() to always return one of
> the elements due to this edge case.

Yes, of course; the alternative implementation I was thinking of
was the one that I implemented eons ago for my own pre-2.5 code,
where I defined any and all roughly as:

any([x1, x2, x3, ...]) <-> False or x1 or x2 or x3 or ...
all([x1, x2, x3, ...]) <-> True and x1 and x2 and x3 and ...

At the time this seemed like the obvious choice, so I was a bit
surprised when it was chosen to always return a bool instead in
the official versions.

Now that I'm older and wise^H^H^H^H, well, maybe just older, the
pure bool version seems cleaner and less error-prone, even if
it's mildly inconsistent with the behaviour of and and or.


More information about the Python-list mailing list