mbi136-176 211% python
*** Pasting of code with ">>>" or "..." has been enabled.
########################################################################
## ipython ##
########################################################################
py> b = 4 if True else b
py> b
4
Isn't the right side supposed to be evaluated first?
Yes, the right hand side of the '=', which is evaluated from LtoR.
>From the Language Reference:
Python evaluates expressions from left to right. Notice that
while evaluating an assignment, the right-hand side is evaluated
before the left-hand side.
So the right-hand side of the '=' is evaluated first:
4 if True else b
And that's evaluated from left to right:
4
Therefore:
b = 4
The right side of the assignment or the if-else expression? The "expr1 if
condition else expr2" expression evaluates condition, then either expr1 or expr2
depending on the result. The other expression is unevaluated.
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
>>> 5 if True else b
5
>>> a if False else 5
5
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
> py> b = 4 if True else b
> py> b
> 4
> Isn't the right side supposed to be evaluated first?
Perhaps it becomes clearer if you change it a bit:
>>> b = 4 if True else whatever
>>> whatever
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'whatever' is not defined
I. e. the else clause is never evaluated at all.
Peter
I don't have a clue what value you expected b to have. The if/else
ternary expression is roughly equivalent to:
if True:
b = 4
else
b = b
The first part to be evaluated is the if expression, which is hard-coded
to True. Then the part to the left will be used for the expression
result, and the part on the right ignored. This is because of
short-circuit rules.
Try this one for size:
b = 22 if True else I.am.Not.Being.Evaluated(4.4)
The else clause does need to be a syntactically valid expression, but
it's not evaluated unless the if-expression is false.
DaveA