Eval of expr with 'or' and 'and' within

Nobody nobody at nowhere.com
Fri Jun 14 12:42:08 EDT 2013


On Fri, 14 Jun 2013 18:16:05 +0300, Nick the Gr33k wrote:

> My question is why the expr (name and month and year) result in the 
> value of the last variable whic is variable year?

For much the same reason that an OR expression returns the first true
value.

"or" and "and" only evaluate as many arguments are required in order to
determine the correct result (aka "short-circuit evaluation"). If the
first argument of "or" is true, or the first argument of "and" is false,
the second argument isn't evaluated (this is important if evaluation can
have side effects).

The operators can be expressed as:

	True or X = True
	False or X = X

	False and X = False
	True and X = X

Note that in the short-circuit case, the result has the same sense (true
or false) as the first argument, while in the other case the result has
the same sense as the second argument.

Python implements these operators by returning the actual value which
determined the result of the expression rather than simply True or False.

If the result is known after evaluating the first argument, the first
argument is returned. If it has to evaluate the second argument, the
second argument is returned (by that point it has already forgotten
the value of the first argument).




More information about the Python-list mailing list