In Python 3, is it possible to use short-circuit evaluation in an any call that doesn't have a for .. in expression in it?
I know that I can take advantage of short-circuit evaluation in a statement like this:
print('Hello' if True or 3/0 else 'World') # this prints "Hello"
But if I try something similar with any, I get an exception because the 3/0 is evaluated:
print('Hello' if any([True, 3/0]) else 'World') # this raises an exception
Is there a way I can use any to do this?
The issue here is not
any, it is the immediate evaluation of3/0as the list literal is evaluated:The only way to avoid that is to delay the evaluation until a later time with the use of a function or iterator: