Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'assignment': 0.07; 'modified': 0.07; '"if': 0.09; 'assigning': 0.09; 'augmented': 0.09; 'similar,': 0.09; 'target,': 0.09; 'python': 0.11; '"an': 0.16; 'in-place': 0.16; 'in-place,': 0.16; 'it".': 0.16; 'rewritten': 0.16; 'language': 0.16; 'wrote:': 0.18; 'possible,': 0.19; 'meant': 0.20; 'creating': 0.23; 'equivalent': 0.26; 'second': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'class': 0.32; 'quite': 0.32; 'says': 0.33; 'actual': 0.34; 'something': 0.35; 'case,': 0.35; 'equal': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'done': 0.36; 'should': 0.36; 'detail': 0.37; 'being': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'expression': 0.60; 'ian': 0.60; 'new': 0.61; 'guarantee': 0.63; 'mar': 0.68; 'guaranteed': 0.75; 'odds': 0.84; 'stronger': 0.84; 'technically': 0.84 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=T8DMdJ16QoVhFUPmXGlRypU/MiZPm53Wur94AiXeTRI=; b=dCWVDJMXURqeBjjUlKhYMdYhzqJ96qXd1a6aXiexh2h6053O2vPIA8bKDLWnySmD2p /LCfkk62xGGaFBHPQsQWB5uth1wdObKCyigsNOowl8Rxxxj0Nz5R9tVeaD3E5drQyPQF EaXjaEbwbUTwrWrZ9gBAdbxq+P2wnHJnCTpIcxxO7z9qp4zfvUgj8RynLV1nAxt/5Dmh xAgcS/Ksp7u+0YYZgy83GDaKAd7PTw5L1scfQlJBvfB6rG1Ec+16HjKnWDSI8jb6wDty B8pY/ngxTEygNY9HyOUvK61KXIveldnuxjjbWH9cpJhpXMuM4cg7a5OalHm9X6EPCvY9 LLcQ== X-Received: by 10.68.201.67 with SMTP id jy3mr36346675pbc.20.1394408603042; Sun, 09 Mar 2014 16:43:23 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Sun, 9 Mar 2014 17:42:42 -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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394408612 news.xs4all.nl 2940 [2001:888:2000:d::a6]:53046 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68107 On Sun, Mar 9, 2014 at 4:03 PM, Gregory Ewing wrote: > Ian Kelly wrote: >> >> In my view the second one is wrong. a += b should be understood as >> being equivalent to a = a + b, but with the *possible* and by no means >> guaranteed optimization that the operation may be performed in-place. > > > This interpretation is at odds with the Language Reference, > section 6.2.1, Augmented Assignment Statements: > > "An augmented assignment expression like x += 1 can be rewritten as x = x + > 1 to > achieve a similar, but not exactly equal effect... when possible, the actual > operation is performed > > in-place, meaning that rather than creating a new object and assigning that > to > the target, the old object is modified instead." > > Note that it says "when possible", not "if the implementation > feels like it". That's quite vague, and not much stronger a guarantee than "maybe". It's technically "possible" for this augmented assignment to be performed in place: x = 12 x += 4 But it's not done in-place, because ints are meant to be immutable. In any case, this means that whether the operation is actually performed in-place is an implementation detail -- if not of the Python implementation then at least of the class -- and not something the user should take for granted.