Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Python quirk in evaluation order

1 view
Skip to first unread message

James Stroud

unread,
Jul 31, 2009, 4:11:49 PM7/31/09
to
Python 2.5:

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?

Albert Hopkins

unread,
Jul 31, 2009, 4:33:00 PM7/31/09
to pytho...@python.org

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

Robert Kern

unread,
Jul 31, 2009, 4:31:21 PM7/31/09
to pytho...@python.org

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

Peter Otten

unread,
Jul 31, 2009, 4:39:25 PM7/31/09
to
James Stroud wrote:

> 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

Dave Angel

unread,
Jul 31, 2009, 5:07:01 PM7/31/09
to James Stroud, pytho...@python.org
James Stroud wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">Python 2.5:

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


0 new messages