Another proposed proposal: operator.bool_and / bool_or

Paul Rubin http
Fri Mar 31 06:13:09 CEST 2006


The suggestion is to add boolean and/or functions to the operator
module, useful with "reduce".  For some reason I thought these were
already present, but they're not (were they removed?).  There are
already and_ and or_, but these are bitwise functions, not boolean
operations.

bool_and and bool_or would differ from the "and" and "or" keywords,
since they're functions and cannot short-circuit.

Specification:

  operator.bool_and(x,y) = (bool(x) & bool(y))
  operator.bool_or(x,y) = (bool(x) | bool(y))

Use case:

   from operator import bool_and, bool_or
   def any(seq): return reduce(bool_or, seq, False)
   def all(seq): return reduce(bool_and, seq, True)

Of course you could use map(bool,seq), but now that we have bools it's
reasonable to have these boolean operations.  



More information about the Python-list mailing list