Another proposed proposal: operator.bool_and / bool_or

P

Paul Rubin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top