Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'received:134': 0.05; 'assignment': 0.07; 'differently': 0.07; 'sys': 0.07; 'augmented': 0.09; 'calculating': 0.09; 'def': 0.12; '"x"': 0.16; '(key,': 0.16; 'assignment.': 0.16; 'behave': 0.16; 'evaluates': 0.16; 'evaluating': 0.16; 'evaluations': 0.16; 'nope,': 0.16; 'reedy': 0.16; 'tab': 0.16; 'value))': 0.16; 'wrote:': 0.18; 'code,': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'case.': 0.24; 'documented': 0.24; 'nearly': 0.26; 'possibly': 0.26; 'header:In- Reply-To:1': 0.27; 'point': 0.28; 'statement': 0.30; 'code': 0.31; "skip:' 10": 0.31; "d'aprano": 0.31; 'fast.': 0.31; 'steven': 0.31; 'class': 0.32; 'run': 0.32; 'quite': 0.32; 'skip:_ 10': 0.34; 'but': 0.35; 'there': 0.35; 'done': 0.36; 'should': 0.36; 'wrong': 0.37; 'to:addr:python-list': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'expression': 0.60; 'new': 0.61; 'simple': 0.61; 'name': 0.63; 'here': 0.66; 'side': 0.67; 'between': 0.67; 'mar': 0.68; 'legal': 0.71; 'hand': 0.80; "'similar'": 0.84 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ap8EAEAqIFOGuA9G/2dsb2JhbABawl6DDIEwgxkBAQEEeBELGAkWDwkDAgECAUUTCAKHdcwJhVkXjik6FoQiBJhFhjWLeIMu Date: Wed, 12 Mar 2014 10:39:35 +0100 From: Antoon Pardon User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20131103 Icedove/17.0.10 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Tuples and immutability References: <0d76f320-a39e-44e9-85a2-74220b646566@googlegroups.com> <531ffe70$0$2923$c3e8da3$76491128@news.astraweb.com> In-Reply-To: <531ffe70$0$2923$c3e8da3$76491128@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394617178 news.xs4all.nl 2904 [2001:888:2000:d::a6]:38763 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68264 Op 12-03-14 07:28, Steven D'Aprano schreef: > 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. The documentation is wrong at that point as the following code illustrates. | import sys | write = sys.stdout.write | | class logdict: | def __init__(self): | self.lst = {} | | def __setitem__(self, key, value): | write('[%s] <= %s\n' % (key, value)) | self.lst[key] = value | | def __getitem__(self, key): | value = self.lst[key] | write('[%s] => %s\n' % (key, value)) | return value | | tab = logdict() | tab['key'] = 'value' | tab['key'] += ' with extra tail' | write('====\n') | tab = logdict() | tab['key'] = 'value' | tab['key'] = tab['key'] + ' with extra tail' If you run this code, you get the following result: | [key] <= value | [key] => value | [key] <= value with extra tail | ==== | [key] <= value | [key] => value | [key] <= value with extra tail As you can see there is no difference here in the evaluations done between using | tab['key'] += ' with extra tail' or | tab['key'] = tab['key'] + ' with extra tail'