Multiple Assignment a = b = c
mr.eightnoteight at gmail.com
Tue Feb 16 11:00:21 EST 2016
On Tue, Feb 16, 2016 at 6:35 PM, Sven R. Kunze <srkunze at mail.de> wrote:
> First, the rhs is evaluated.
> Second, the lhs is evaluated from left to right.
Great, I will remember these two lines :)
On Tue, Feb 16, 2016 at 8:46 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> _temp = c
> a = _temp
> b = _temp
> del _temp
> except the name "_temp" isn't actually used.
So it is like first right most expression is evaluated and then lhs is
evaluated from left to right.
> py> from dis import dis
> py> code = compile("a = L[a] = x", "", "exec")
> py> dis(code)
> 1 0 LOAD_NAME 0 (x)
> 3 DUP_TOP
> 4 STORE_NAME 1 (a)
> 7 LOAD_NAME 2 (L)
> 10 LOAD_NAME 1 (a)
> 13 STORE_SUBSCR
> 14 LOAD_CONST 0 (None)
> 17 RETURN_VALUE
> Translated to English:
> - evaluate the expression `x` and push the result onto the stack;
> - duplicate the top value on the stack;
> - pop the top value off the stack and assign to name `a`;
> - evaluate the name `L`, and push the result onto the stack;
> - evaluate the name `a`, and push the result onto the stack;
> - call setattr with the top three items from the stack.
thank-you so much, for explaining how to find the underlying details.
>> SO isn't it counter intuitive from all other python operations.
>> like how we teach on how python performs a swap operation???
> No. Let's look at the equivalent swap:
> In all cases, the same rule applies:
> - evaluate the right hand side from left-most to right-most, pushing the
> values onto the stack;
> - perform assignments on the left hand side, from left-most to right-most.
uhh, i related it with swap because I was thinking variables are
binded, like first of all for all lhs assignments get their references
or names and then put the value of rhs in them.
as `a` is a name, so the rhs reference is copied to the a
`roots[a]` is a reference to an object, so it is initialized with the
reference of rhs.
anyway I got it, and all my further doubts are cleared from that
compiled code. I tried some other examples and understood how it
thanks a lot.
Junior (3rd yr) student at Indian School of Mines,(IIT Dhanbad)
Computer Science and Engineering Department
ph: +91 9491 383 249
More information about the Python-list