Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent short-circuit evaluation?

This is a problem that occurred to me while working on a Django project. It's about form validation.

In Django, when you have a submitted form, you can call is_valid() on the corresponding form object to trigger the validation and return a Boolean value. So, usually you have code like that inside your view functions:

if form.is_valid():     # code to save the form data 

is_valid() not only validates the form data but also adds error messages to the form object that can afterwards be displayed to the user.

On one page I use two forms together and also want the data to be saved only if both forms contain valid data. That means I have to call is_valid() on both forms before executing the code to save the data. The most obvious way:

if form1.is_valid() and form2.is_valid():     # ... 

won't work because of the short circuit evaluation of logical operators. If form1 is not valid, form2 will not be evaluated and its error messages would be missing.

That's only an example. As far as I know, there is no greedy alternative to and/or as in other languages (i.e. Smalltalk). I can imagine that problem occurring under different circumstances (and not only in Python). The solutions I could think of are all kind of clumsy (nested ifs, assigning the return values to local variables and using them in the if statement). I would like to know the pythonic way to solve this kind of problem.

like image 763
j0ker Avatar asked Sep 05 '12 12:09

j0ker


People also ask

What is short-circuit evaluation?

Short-Circuit Evaluation: Short-circuiting is a programming concept in which the compiler skips the execution or evaluation of some sub-expressions in a logical expression. The compiler stops evaluating the further sub-expressions as soon as the value of the expression is determined.

Which of the following operators can be used to do a short-circuit evaluation?

The logical AND operator performs short-circuit evaluation: if the left-hand operand is false, the right-hand expression is not evaluated. The logical OR operator also performs short-circuit evaluation: if the left-hand operand is true, the right-hand expression is not evaluated.

What is short-circuit evaluation for AND and OR operators?

Short-circuit evaluation The logical AND expression is a short-circuit operator. As each operand is converted to a boolean, if the result of one conversion is found to be false , the AND operator stops and returns the original value of that falsy operand; it does not evaluate any of the remaining operands.

What does it mean to short-circuit an evaluation provide an example use case?

Short-circuiting the evaluation of an expression means that only a part of the expression needs to be evaluated before finding its value. For example: a == null || a.size() == 0. If a is null , the a.


2 Answers

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).

like image 59
mgilson Avatar answered Sep 29 '22 01:09

mgilson


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():    ... 
like image 31
sloth Avatar answered Sep 29 '22 00:09

sloth