Equivalent code to the bool() built-in function

Jean-Paul Calderone calderone.jeanpaul at gmail.com
Tue Apr 19 11:43:36 EDT 2011


On Apr 19, 10:23 am, Grant Edwards <inva... at invalid.invalid> wrote:
> On Tue, Apr 19, 2011 at 7:09 AM, Christian Heimes <li... at cheimes.de> wrote:
> > Am 18.04.2011 21:58, schrieb John Nagle:
> >> ?? ?? This is typical for languages which backed into a "bool" type,
> >> rather than having one designed in. ??The usual result is a boolean
> >> type with numerical semantics, like
>
> >> ??>>> True + True
> >> 2
>
> > I find the behavior rather useful. It allows multi-xor tests like:
>
> > if a + b + c + d != 1:
> > ?? ??raise ValueError("Exactly one of a, b, c or d must be true.")
>
> I guess I never thought about it, but there isn't an 'xor' operator to
> go along with 'or' and 'and'.  Must not be something I need very often.
>

You also can't evaluate xor without evaluating both operands, meaning
there
is never a short-circuit; both and and or can short-circuit, though.
Also
boolean xor is the same as !=.

Jean-Paul



More information about the Python-list mailing list