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


Groups > comp.lang.python > #103017

Re: Multiple Assignment a = b = c

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From srinivas devaki <mr.eightnoteight@gmail.com>
Newsgroups comp.lang.python
Subject Re: Multiple Assignment a = b = c
Date Tue, 16 Feb 2016 21:30:21 +0530
Lines 81
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>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de 8QQTF9XtLNQEta9zKCFyQg5vkhHs2W4H91S/yHvN/3Sw==
Return-Path <mr.eightnoteight@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'pop': 0.05; 'used.': 0.05; 'performs': 0.07; 'stack.': 0.07; 'works.': 0.07; 'underlying': 0.09; 'python': 0.10; 'anyway': 0.11; 'stack': 0.13; '+91': 0.15; 'variables': 0.15; '2016': 0.16; 'assignments': 0.16; 'evaluated.': 0.16; 'lhs': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'rhs': 0.16; 'stack;': 0.16; 'thank-you': 0.16; 'wrote:': 0.16; 'copied': 0.18; 'duplicate': 0.18; 'student': 0.20; 'first,': 0.20; '"",': 0.22; '(a)': 0.22; 'assign': 0.22; 'junior': 0.22; 'code.': 0.23; 'feb': 0.23; 'references': 0.23; 'tried': 0.24; 'import': 0.24; 'examples': 0.24; 'header:In-Reply-To:1': 0.24; 'equivalent': 0.27; 'right.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'object,': 0.27; 'translated': 0.27; 'values': 0.28; 'initialized': 0.29; 'lot.': 0.29; 'second,': 0.29; 'code': 0.30; 'push': 0.30; 'related': 0.32; 'compiled': 0.32; "d'aprano": 0.33; 'operations.': 0.33; 'rule': 0.33; 'steven': 0.33; 'tue,': 0.34; 'except': 0.34; 'received:google.com': 0.35; "isn't": 0.35; 'lines': 0.36; 'received:209.85': 0.36; 'indian': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'thanks': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'names': 0.38; 'to:addr:python.org': 0.40; 'some': 0.40; 'no.': 0.62; 'school': 0.62; 'side': 0.62; 'further': 0.62; 'teach': 0.70; 'evaluate': 0.72; 'hand': 0.82; '(3rd': 0.84; 'doubts': 0.84; 'ph:': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=cn2CNENbT92qR9anqmg2YUcnrbvIy5KT1L1m+y/m71o=; b=YAPeu0MgSD5PQtljSlv+kMQuFn3botuxpUQZrAMf5MoCVp7GUPCVkA7CGfFyK7HsVv /hTTa1EUu4SixujAJ7B+n6hvQUN7H8P6NHTdAIlpfwFW0zvYTmFvNCbqo7yoNH7sdrKO R/UgcnQLPqfCGlAdB39uc2iwc+no3kiv68U9/VTu9tIh6Q6171HLVjsTgcfuUYJJeaar 7Ae7Oe20OneP0H2tRMnYX+bveHAxyY8gfXc6e7Jz0mrKsJKeN0sS6S8z++3RhYqLYGdL Bg5Qu0U6wBUPVra+BLB8Np2FpCLbe5ibMgySH/EJhgmIgCUcjgHvX6a/JsikVLyYEwr0 R54w==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=cn2CNENbT92qR9anqmg2YUcnrbvIy5KT1L1m+y/m71o=; b=m1aM6RJK1FebzBqawcmJba23Z4byD6yeo+Uz8t3+0MFkGzILk+Otqpfs/7GDmdte8p VNQdtdtUS/1nIgSWnTR0bEJmcPv2chfpAYNzw0D/GzTQ+A5XeptGghMhCZ9zapibtJjm hlkbB3xiDc6N3B7nc1pE1/0acn2EYP7DqpNcICIiuriO5abZAnMSz+dNF4av+oygr+IT VCTp8hM3RIO26Xk0ombbFtQa+ofOizo5KJqlfhj7lH1R1UA09d0pp/ZEbt+hCbTX0gQn iEHZl7jrzNMNZ1IUGQjtPojkPWOLe58d7o+ceOfqshbKWKPjHELuHe1KBqWnH9UD4mLx Ku3w==
X-Gm-Message-State AG10YOTIpptFpOvxQuAySyzJMbEEEmFjGW2qBOIt2zk27NQ7mZ43aHxSsB3+doMJl8ZZV8/s1sKlSqLcvJM3pA==
X-Received by 10.50.29.51 with SMTP id g19mr17291300igh.41.1455638460670; Tue, 16 Feb 2016 08:01:00 -0800 (PST)
In-Reply-To <56c33d48$0$1587$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21rc2
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:103017

Show key headers only | 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