From the Python 3.5.2 docs:

6.15. 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.

Thus, spam = eggs = cheese = obj is equivalent to:

spam = (eggs = (cheese = obj))

which is also equivalent to:

cheese = obj
eggs = cheese
spam = eggs

These all are now references to obj.

In both of your examples, everything also ends up as references to obj. The only difference is the order of evaluation.

Bev in TX

On 11/19/16 12:58 AM, Chris Angelico wrote:
On Sat, Nov 19, 2016 at 3:34 PM, Steve D'Aprano
<steve+pyt...@pearwood.info> wrote:
What happens if you do this?

spam = eggs = cheese = obj

Is that different from:

spam = obj
eggs = obj
cheese = obj


or from this?

spam = obj
eggs = spam
cheese = eggs
...
These aren't silly questions.

Indeed, they are not silly. It surprised me (as a C programmer) that
the assignments happen left-to-right, rather than right-to-left (in C,
cheese would be set first, then eggs, then spam). These are questions
filled with subtleties.

ChrisA

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to