Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103026 > unrolled thread
| Started by | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| First post | 2016-02-16 14:31 -0500 |
| Last post | 2016-02-16 14:31 -0500 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Multiple Assignment a = b = c Terry Reedy <tjreedy@udel.edu> - 2016-02-16 14:31 -0500
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2016-02-16 14:31 -0500 |
| Subject | Re: Multiple Assignment a = b = c |
| Message-ID | <mailman.180.1455651096.22075.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web