Interpreting Left to right?

Ethan Furman ethan at stoneleaf.us
Fri Jun 24 03:14:27 EDT 2011


Terry Reedy wrote:
> On 6/24/2011 12:32 AM, Chetan Harjani wrote:
>> x=y="some string"
>> And we know that python interprets from left to right.
> 
> Read the doc. "5.14. Evaluation order
> Python evaluates expressions from left to right. Notice that while 
> evaluating an assignment, the right-hand side is evaluated before the 
> left-hand side."

The example given to me when I had this question:

--> x = x['huh'] = {}
--> x
{'huh': {...}}


As you can see, the creation of the dictionary is evaluated, and bound 
to the name 'x'; then the key 'huh' is set to the same dictionary.  If 
you try that the other way 'round this happens:

 >>> x['huh'] = x = {}
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

So -- the RHS (right hand side) gets evaluated first, then the LHSs from 
left to right.

~Ethan~



More information about the Python-list mailing list