Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103026
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Terry Reedy <tjreedy@udel.edu> |
| Newsgroups | comp.lang.python |
| Subject | Re: Multiple Assignment a = b = c |
| Date | Tue, 16 Feb 2016 14:31:00 -0500 |
| Lines | 73 |
| Message-ID | <mailman.180.1455651096.22075.python-list@python.org> (permalink) |
| References | <CACs7g=C6AZVOtFg7ufNo09jt3oG006GH=onNbY3SH6Oc_TqYOw@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de ZFjrwHUII8jZKHNMk7avBQslKVkN16+3hSiZggglEw7w== |
| Return-Path | <python-python-list@m.gmane.org> |
| 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; 'resulting': 0.04; 'assignment': 0.07; 'valueerror:': 0.07; '(1,': 0.09; 'nameerror:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'unpack': 0.09; 'python': 0.10; 'jan': 0.11; 'output': 0.13; 'def': 0.13; '"an': 0.16; "'c'": 0.16; '(remember': 0.16; 'assigns': 0.16; 'at- least': 0.16; 'chained': 0.16; 'evaluates': 0.16; 'iterator.': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'tuple)': 0.16; 'wrote:': 0.16; 'resolved': 0.18; '>>>': 0.20; '"",': 0.22; 'latter': 0.22; 'variables.': 0.22; 'am,': 0.23; 'defined': 0.23; 'references': 0.23; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'header:X-Complaints-To:1': 0.26; 'yield': 0.27; 'values': 0.28; 'fixed': 0.31; 'skip:_ 10': 0.32; 'statement': 0.32; 'class': 0.33; 'url:python': 0.33; 'traceback': 0.33; 'file': 0.34; 'list': 0.34; 'done': 0.35; 'but': 0.36; 'list,': 0.36; 'url:org': 0.36; 'received:71': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'skip:p 20': 0.38; 'hi,': 0.38; 'does': 0.39; 'enough': 0.39; 'to:addr:python.org': 0.40; 'url:3': 0.60; 'great': 0.63; 'hours': 0.65; 'today': 0.65; 'ruled': 0.84; 'tem': 0.84; 'received:fios.verizon.net': 0.91; 'url:reference': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | pool-71-185-227-36.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 |
| In-Reply-To | <CACs7g=C6AZVOtFg7ufNo09jt3oG006GH=onNbY3SH6Oc_TqYOw@mail.gmail.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:103026 |
Show key headers only | View raw
On 2/16/2016 7:46 AM, srinivas devaki wrote:
> Hi,
>
> a = b = c
>
> as an assignment doesn't return anything, i ruled out a = b = c as
> chained assignment, like a = (b = c)
> SO i thought, a = b = c is resolved as
> a, b = [c, c]
https://docs.python.org/3/reference/simple_stmts.html#assignment-statements
"An assignment statement evaluates the expression list (remember that
this can be a single expression or a comma-separated list, the latter
yielding a tuple) and assigns the single resulting object to each of the
target lists, from left to right."
a = b = c is the same as tem = c; a = tem; b = tem.
This does not work if tem is an iterator.
>>> def g():
yield 1
yield 2
>>> a,b = c,d = g()
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
a,b = c,d = g()
ValueError: not enough values to unpack (expected 2, got 0)
>>> a, b
(1, 2)
>>> c,d
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
c,d
NameError: name 'c' is not defined
> at-least i fixed in my mind that every assignment like operation in
> python is done with references and then the references are binded to
> the named variables.
> like globals()['a'] = result()
>
> but today i learned that this is not the case with great pain(7 hours
> of debugging.)
>
> class Mytest(object):
> def __init__(self, a):
> self.a = a
> def __getitem__(self, k):
> print('__getitem__', k)
> return self.a[k]
> def __setitem__(self, k, v):
> print('__setitem__', k, v)
> self.a[k] = v
>
> roots = Mytest([0, 1, 2, 3, 4, 5, 6, 7, 8])
> a = 4
> roots[4] = 6
> a = roots[a] = roots[roots[a]]
tem = roots[roots[a]]
a = tem
roots[a] = tem
> the above program's output is
> __setitem__ 4 6
> __getitem__ 4
> __getitem__ 6
> __setitem__ 6 6
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Multiple Assignment a = b = c Terry Reedy <tjreedy@udel.edu> - 2016-02-16 14:31 -0500
csiph-web