Your code errors because the expressions have to be evaluated before being passed into the function.
In this context, short-circuiting actually means that as soon as they find a different value, they return and don't bother checking the remaining values. It's not the same kind of short-circuiting that and
and or
do, which can actually avoid evaluating expressions.
To illustrate, let's use an iterator, which will get consumed:
>>> a = iter([1, 0, 2, 3])
>>> all(a) # Search until a falsy value
False
>>> list(a) # Show what's left in the iterator
[2, 3]
You can see that it consumed 1
and 0
, returning on 0
because it's falsy.
Now, if you did want to do lazy evaluation with any
and all
, you could call lambdas in a generator expression:
>>> any(e() for e in [lambda: 1, lambda: 0, lambda: 2/0])
True
>>> all(e() for e in [lambda: 1, lambda: 0, lambda: 2/0])
False
(Thanks to this question for inspiring this bit.)