Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Sven R. Kunze" Newsgroups: comp.lang.python Subject: Re: Multiple Assignment a = b = c Date: Tue, 16 Feb 2016 14:05:34 +0100 Lines: 64 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de vqQuS3s81pcVIn3wAgmfuwJ/PQ4/MGh3XuYldeZq6+Zg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'assignment': 0.07; 'performs': 0.07; 'tuple': 0.09; 'python': 0.10; 'output': 0.13; 'def': 0.13; 'at-least': 0.16; 'chained': 0.16; 'evaluated.': 0.16; 'lhs': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'rhs': 0.16; 'wrote:': 0.16; 'basically': 0.18; 'resolved': 0.18; 'first,': 0.20; 'variables.': 0.22; 'references': 0.23; 'header :In-Reply-To:1': 0.24; "doesn't": 0.26; 'right.': 0.27; 'idea': 0.28; 'this.': 0.28; 'second,': 0.29; 'fixed': 0.31; 'skip:_ 10': 0.32; 'class': 0.33; 'operations.': 0.33; 'received:10.0': 0.34; 'best,': 0.35; 'done': 0.35; 'expected': 0.35; "isn't": 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'skip:p 20': 0.38; 'hi,': 0.38; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'charset:windows-1252': 0.62; 'great': 0.63; 'hours': 0.65; 'today': 0.65; 'teach': 0.70; 'ruled': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mail.de; s=mail201212; t=1455627937; bh=6C+ZvEGJuX60iPEPnZeK6SHbrQiXrGSjypssCcXK5m8=; h=Subject:To:References:From:Date:In-Reply-To:From; b=FKKTnxmz3As+wX8LV+5AGoJQs8uU15zku0JY2beD7DttjcN24mp5UaBEm84Cw0w7g d/jZcl2YmlNgE4myZ9zQWoUU/glGTvyWcapy7fHRlwV0ZSGcDHTEdyvncUTFtIKfMG XAD6Z6f7qE1cw5VOJASFkJxw7DN+skV5Sv4KiHrs= In-Reply-To: X-purgate: clean X-purgate: This mail is considered clean (visit http://www.eleven.de for further information) X-purgate-type: clean X-purgate-Ad: Categorized by eleven eXpurgate (R) http://www.eleven.de X-purgate: This mail is considered clean (visit http://www.eleven.de for further information) X-purgate: clean X-purgate-size: 1542 X-purgate-ID: 154282::1455627936-00004575-A9F2D0C0/0/0 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103002 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