Python: Avoid short circuit evaluation
How about something like:
if all([form1.is_valid(), form2.is_valid()]):
...
In a general case, a list-comprehension could be used so the results are calculated up front (as opposed to a generator expression which is commonly used in this context). e.g.:
if all([ form.is_valid() for form in (form1,form2) ])
This will scale up nicely to an arbitrary number of conditions as well ... The only catch is that they all need to be connected by "and
" as opposed to if foo and bar or baz: ...
.
(for a non-short circuiting or
, you could use any
instead of all
).
You can simply use the binary &
operator, which will do a non-short-circuit logical AND on bools.
if form1.is_valid() & form2.is_valid():
...