missing 'xor' Boolean operator

Christian Heimes lists at cheimes.de
Tue Jul 14 19:08:24 EDT 2009


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




More information about the Python-list mailing list