Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103017

Re: Multiple Assignment a = b = c

From srinivas devaki <mr.eightnoteight@gmail.com>
Newsgroups comp.lang.python
Subject Re: Multiple Assignment a = b = c
Date 2016-02-16 21:30 +0530
Message-ID <mailman.176.1455638463.22075.python-list@python.org> (permalink)
References <mailman.161.1455626848.22075.python-list@python.org> <56c33d48$0$1587$c3e8da3$5496439d@news.astraweb.com>

Show all headers | View raw


On Tue, Feb 16, 2016 at 6:35 PM, Sven R. Kunze <srkunze@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@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
works.

thanks a lot.

-- 
Regards
Srinivas Devaki
Junior (3rd yr) student at Indian School of Mines,(IIT Dhanbad)
Computer Science and Engineering Department
ph: +91 9491 383 249
telegram_id: @eightnoteight

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Multiple Assignment a = b = c srinivas devaki <mr.eightnoteight@gmail.com> - 2016-02-16 18:16 +0530
  Re: Multiple Assignment a = b = c Steven D'Aprano <steve@pearwood.info> - 2016-02-17 02:16 +1100
    Re: Multiple Assignment a = b = c srinivas devaki <mr.eightnoteight@gmail.com> - 2016-02-16 21:30 +0530

csiph-web