Eval of expr with 'or' and 'and' within
Grant Edwards
invalid at invalid.invalid
Fri Jun 14 15:30:27 EDT 2013
On 2013-06-14, Nobody <nobody at nowhere.com> wrote:
> 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).
There are two completely orthogonal concepts here:
1. Short-circuit evaluation. Many languages do this. AFAICT, this
isn't what he's asking about, but this is what people keep
explaining.
2. Returning one the objects that result from the evaluation of the
operands instead of returning True or False.
This is what seems to be confusing him. This is much less common
than short-circuit evaluation. C does short-circuit evaluation of
&& and || operators, but the result is always 1 or 0 (true of
false). Instead of always returning True or False (which could be
done and still preserver short-circuit evaluation), Python returns
one of the operands or False.
If you also have 1. there are cases where the value returned by
the "or" operator is useful apart from it's "truthyness" value.
There may be cases where the result returned by the "and" operator
is useful apart from it's truthyness, but that seems to be less
common. Taking advantage of that fact can lead to some
hard-to-read code, so it's often discouraged as being too clever.
It's important to note that these are somewhat orthogonal:
You can have #1 and #2 (like Python).
You can have #1 without #2 (like C).
You can have #2 without #1
You can have neither #1 or #2
But again, #2 is more useful if you also have #1.
--
Grant Edwards grant.b.edwards Yow! ... I have read the
at INSTRUCTIONS ...
gmail.com
More information about the Python-list
mailing list