Is there a builtin function version of `and` and/or `or` in Python?
Question: This question is for fun; I don't expect the answer to be useful.

When I see people doing things with `reduce()` in Python, they often take
advantage of a builtin function in Python, often from the `operator` module.

This works:

    
    
    result = reduce(lambda a, b: a + b, range(5))
    

But usually you would see this:

    
    
    from operator import add
    result = reduce(add, range(5))
    

What's strange to me is that the `operator` module doesn't seem to have a
function for logical `and`. It does have _bitwise_ and, but not logical `and`.

So suppose you are doing this:

    
    
    result = reduce(lambda a, b: a and b, range(1, 6))
    

Is there a builtin function that can be used here?

I'm also wondering if there is a builtin function that can replace `or`.

If you map the arguments to Booleans first, you can use the bitwise and from
`operator`, or just directly use `bool.__and__`, like so:

    
    
    from operator import and_
    result = reduce(and_, map(bool, range(1, 6)))
    result = reduce(bool.__and__, map(bool, range(1, 6)))
    

And likewise with `operator.or_()` or `bool.__or__` for the `or` operation.
But I am looking for a function that doesn't need the values mapped to
Booleans.

If you knew for certain that your values are all integers, you could use
`operator.mul` for `and` and `operator.add` for `or`. This would be a crude
hack and I don't want this answer... especially considering how expensive the
multiplications would get if many numbers were encountered and none of them
were zero!

Note: I am aware of `all()` and `any()`, which are better replacements for
this use of `reduce()`. As I said at the top, I'm asking this for fun.

Note: A function that has the side-effect of forcing all values to `bool`
would be an acceptable answer. The built-in `and` keyword doesn't do this:

    
    
    x = 3 and 5  # sets x to 5, not to True
    

But for the purpose of this question I am just interested in a function that
can work with `reduce()` to do logical `and` or `or` operations.


Answer: I guess the actual reason why there's no `and` and `or` in the operator module
is that it's impossible to evaluate function arguments in a short-circuit
fashion - which is the whole point of boolean operators. So the answer to your
question is no, there's no built-in function that can mimic `and/or` nor is it
possible to write one.

`all/any` applied to generators are short-circuiting as well

    
    
    def gen():
        yield 1
        yield this_wont_be_evaluated
    
    print any(gen())
    

but I have no idea how make that work with run-time arguments

