Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed5.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'thread,': 0.04; 'interpreter': 0.07; 'method,': 0.07; 'raises': 0.07; 'received:verizon.net': 0.07; 'terry': 0.07; 'correct.': 0.09; 'immutable': 0.09; 'mutable': 0.09; 'optionally': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'subject:" ': 0.10; 'examples': 0.11; 'def': 0.15; 'method.': 0.15; '(1,': 0.16; '[])': 0.16; 'assignment.': 0.16; 'containers': 0.16; 'mutated': 0.16; 'numpy': 0.16; 'reedy': 0.16; 'result:': 0.16; 'subject:=': 0.16; 'surrogates.': 0.16; 'wrote:': 0.16; 'jan': 0.19; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'optional': 0.23; 'pm,': 0.24; 'effect': 0.28; 'correct': 0.28; 'import': 0.28; '(and': 0.29; 'updated': 0.29; 'example': 0.30; 'arrays': 0.30; 'augmented': 0.30; 'extends': 0.30; 'object.': 0.30; 'typeerror:': 0.30; 'class': 0.30; 'error': 0.32; 'references': 0.32; 'break': 0.32; 'does': 0.32; 'there': 0.33; 'to:addr:python- list': 0.33; 'instead': 0.33; 'done': 0.34; 'header:User-Agent:1': 0.34; 'nobody': 0.34; 'normally': 0.34; 'test': 0.34; 'assignment': 0.34; 'header:X-Complaints-To:1': 0.35; 'rather': 0.35; 'object': 0.35; 'unless': 0.36; 'but': 0.37; 'two': 0.37; 'received:org': 0.38; 'purposes': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; 'extend': 0.39; 'to:addr:python.org': 0.39; 'subject: (': 0.39; 'target': 0.61; 'subject': 0.61; 'results': 0.61; 'expectations': 0.73; 'subject:+': 0.73; '*does*': 0.84; '[1,2]': 0.84; 'different.': 0.84; 'other):': 0.84; 'other.n': 0.84; 'self.n': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Date: Sun, 21 Aug 2011 20:35:32 -0400 References: <8c606fc1-0aa8-4113-b607-e46ad6f3d649@glegroupsg2000goo.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 84 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313974278 news.xs4all.nl 23972 [2001:888:2000:d::a6]:38162 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11978 On 8/21/2011 5:07 PM, Nobody wrote: > If the value on the left has an __iadd__ method, that will be called; Correct > the value will be updated in-place, Not necessarily correct. The target is rebound to the return from the __iadd__ method. Augmented *assignment* is always assignment. This trips up people who try t = (1, []) t[1] += [1,2] # which *does* extend the array, but raises TypeError: 'tuple' object does not support item assignment # instead of t[1].extend([1,2]) # which extends without raising an error *IF* (and only if) the target object is mutable, then the __iadd__ may optionally mutate the target object. But the rebinding takes place nonetheless. Numbers, the subject of this thread, are not mutable and are not 'updated in-place' class test: def __init__(self, n): self.n = n def __iadd__(self, other): return test(self.n + other.n) def __repr__(self): return repr(self.n) r = test(1) t = r t += r print(r, t) # 1,2 That said, there is normally no reason to write an __ixxx__ method unless instances are mutable and the operation can be and is done in place therein. So the class above is for illustrative purposes only. A saner example is the following, which treats test examples as mutable number containers rather than as immutable surrogates. class test: def __init__(self, n): self.n = n def __add__(self, other): return test(self.n + other.n) def __iadd__(self, other): n = self.n + other.n self.n = n return n def __repr__(self): return repr(self.n) r = test(1) t = r t += r print(r, t) # 2 2 The interpreter cannot enforce that 'x += a' have the same effect as 'x = x+a', but it would break normal expectations to make the two different. > so all references to that object will be affected: Only if the target object is mutable and is mutated by the optional augmented assignment __ixxx__ methods. > > import numpy as np > > a = np.zeros(3) Numpy arrays meet the qualification above. > If the value on the left doesn't have an __iadd__ method, then addition is > performed and the name is re-bound to the result: As is also done with the results of __ixxx__ methods. -- Terry Jan Reedy