Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.028 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'assignment': 0.07; 'differently': 0.07; 'augmented': 0.09; 'calculating': 0.09; 'prevents': 0.09; '"x"': 0.16; 'assignment.': 0.16; 'behave': 0.16; 'evaluates': 0.16; 'evaluating': 0.16; 'lhs': 0.16; 'nope,': 0.16; 'reedy': 0.16; 'storing': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'case.': 0.24; 'documented': 0.24; 'nearly': 0.26; 'possibly': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'am,': 0.29; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; "d'aprano": 0.31; 'fast.': 0.31; 'loading': 0.31; 'steven': 0.31; 'quite': 0.32; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'should': 0.36; 'to:addr :python-list': 0.38; 'anything': 0.39; '12,': 0.39; 'to:addr:python.org': 0.39; 'expression': 0.60; 'new': 0.61; 'course': 0.61; 'simple': 0.61; 'name': 0.63; 'side': 0.67; 'mar': 0.68; 'legal': 0.71; 'hand': 0.80; "'similar'": 0.84; 'evaluation.': 0.84; 'side-effects': 0.84; 'do:': 0.91 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=g7CWihqbtjKrCJdCH+7/5Oyjh24VljoGCEIM+WGc18k=; b=DS6OUmpzsWwuGfDMBAo6H4PgtEMmlWvD70Myb/aBtdc6ylFBloP2DQf1zF1oG/AdX5 7wbR1l7xQNlksP8MxfSlErC4gJhDxFr2PNg/5/619F6b/IaWsN/MNOhEyJToE3CH6EKv OX/sas0ot2kpoH+XBfPc9zvuKZXrvDKckH0C15VZaz+2ZV89g9WrBBQteUUzWLOeRfyf k4mm88ljPN4jdMZamEGLHyYKzjPvSPugNMGrVwdA24lzs5mA2N22KjZLRuEdmv12pas5 Smd5TbSZo7Fcx6RsxPDFIg1bCD/wWaK/MwNnlLgAa/WCKmpObkFbnO7lVToQlLFJ06XH IbcA== X-Received: by 10.68.170.66 with SMTP id ak2mr3737581pbc.5.1394617269540; Wed, 12 Mar 2014 02:41:09 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <531ffe70$0$2923$c3e8da3$76491128@news.astraweb.com> References: <0d76f320-a39e-44e9-85a2-74220b646566@googlegroups.com> <531ffe70$0$2923$c3e8da3$76491128@news.astraweb.com> From: Ian Kelly Date: Wed, 12 Mar 2014 03:40:29 -0600 Subject: Re: Tuples and immutability To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394617273 news.xs4all.nl 2964 [2001:888:2000:d::a6]:40684 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68265 On Wed, Mar 12, 2014 at 12:28 AM, Steven D'Aprano wrote: > On Tue, 11 Mar 2014 23:25:19 -0400, Terry Reedy wrote: >> Nope, 'similar' is not 'equivalent'. Evaluating x twice instead of once >> and possibly allocating a new object versus not take extra time. In a >> statement like "x.y.z[3*n+m] += 1", calculating the target dominates the >> time to increment, so this form should be nearly twice as fast. > > Excellent point Terry! > > I always forget that the target of an augmented assignment may not be a > simple name like "x" but can be an arbitrary complex reference, anything > that is a legal assignment target. Because += is documented as only > evaluating the expression once it can behave quite differently to the > `spam = spam + 1` case. Evaluating the right hand side may have side- > effects that change what the left hand side evaluates to. This is not the > case with the augmented assignment. Of course one could also do: z = x.y.z i = 3*n+m z[i] = z[i] + 1 which reduces the duplicated work down to storing and loading a couple of locals, and also prevents side-effects from affecting the LHS evaluation.