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


Groups > comp.lang.python > #103002 > unrolled thread

Re: Multiple Assignment a = b = c

Started by"Sven R. Kunze" <srkunze@mail.de>
First post2016-02-16 14:05 +0100
Last post2016-02-16 14:05 +0100
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.


Contents

  Re: Multiple Assignment a = b = c "Sven R. Kunze" <srkunze@mail.de> - 2016-02-16 14:05 +0100

#103002 — Re: Multiple Assignment a = b = c

From"Sven R. Kunze" <srkunze@mail.de>
Date2016-02-16 14:05 +0100
SubjectRe: Multiple Assignment a = b = c
Message-ID<mailman.163.1455627938.22075.python-list@python.org>
Hi Srinivas,

On 16.02.2016 13:46, 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]
>
>
> 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]]
>
>
> the above program's output is
> __setitem__ 4 6
> __getitem__ 4
> __getitem__ 6
> __setitem__ 6 6
>
>
> But the output that i expected is
> __setitem__ 4 6
> __getitem__ 4
> __getitem__ 6
> __setitem__ 4 6
>
> SO isn't it counter intuitive from all other python operations.
> like how we teach on how python performs a swap operation???
>
> I just want to get a better idea around this.

I think the tuple assignment you showed basically nails it.

First, the rhs is evaluated.
Second, the lhs is evaluated from left to right.

Completely wrong?

Best,
Sven

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web