missing 'xor' Boolean operator

pdpi pdpinheiro at gmail.com
Wed Jul 15 11:17:09 CEST 2009


On Jul 15, 12:08 am, Christian Heimes <li... at cheimes.de> wrote:
> Chris Rebert wrote:
> > Using the xor bitwise operator is also an option:
> > bool(x) ^ bool(y)
>
> I prefer something like:
>
>     bool(a) + bool(b) == 1
>
> It works even for multiple tests (super xor):
>
>   if bool(a) + bool(b) + bool(c) + bool(d) != 1:
>       raise ValueError("Exactly one of a, b, c and d must be true")
>
> Christian

"if bool(a) + bool(b) + bool(c) + bool(d) != 1:" is not equivalent to
xor. 1 xor 1 xor 1 = 1 xor (1 xor 1) = 1 xor 0 = 1 (or = (1 xor 1) xor
1 = 0 xor 1 = 1 if you assicate to the left)



More information about the Python-list mailing list