Is there a way to short circuit an all() statement in Python?
So something like this:
return all([x != 0, 10 / x == 2, True, False, 7])
won’t return an error if x is 0?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That doesn’t work because the list (that is, all its items) is evaluated before
allis even called. You best option in this particular case is something like this:I assume you already know this, so I’ll provide you with another, more general solution that can also be applied if the list items are not hardcoded:
That way the expressions will only be evaluated within the short-circuit
all.