Path: csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"""': 0.07; 'assignment': 0.07; 'binary': 0.07; 'modified': 0.07; 'plenty': 0.07; '[1,': 0.09; 'assigning': 0.09; 'augmented': 0.09; 'performs': 0.09; 'referenced': 0.09; 'similar,': 0.09; 'target,': 0.09; 'cc:addr :python-list': 0.11; '3],': 0.16; 'effect.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'in- place,': 0.16; 'once.': 0.16; 'ought': 0.16; 'rewritten': 0.16; 'simplest': 0.16; 'wrote:': 0.18; 'possible,': 0.19; 'thu,': 0.19; 'later': 0.20; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'instead.': 0.24; 'cc:2**0': 0.24; 'references': 0.26; 'post': 0.26; 'header:In-Reply-To:1': 0.27; 'feature': 0.29; 'message-id:@mail.gmail.com': 0.30; 'url:python': 0.33; 'actual': 0.34; "can't": 0.35; 'equal': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'raising': 0.36; 'url:org': 0.36; 'two': 0.37; 'form,': 0.38; 'version,': 0.38; 'pm,': 0.38; 'rather': 0.38; 'expression': 0.60; 'new': 0.61; 'url:3': 0.61; 'skip:* 10': 0.61; 'first': 0.61; 'skip:n 10': 0.64; 'here': 0.66; 'link:': 0.72; 'jul': 0.74; 'comparable': 0.84; 'distinguish': 0.84; 'object:': 0.84; 'significance': 0.84; 'url:reference': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=dQtehWaIKpcJbNFqNniXESdXovlXi6yCbYZEofWoht0=; b=ylLSJgPT3wT6SFOtTggSfYihbOgoAwrwgD98obz0Uidf4ai/NaKIKPD9RszJZNnltX X3NvaBhm4YNwkdIfR1ogndGqKAU/DHO3/JqNvsGqGsa01fUUMl2yMLoZSJhE0dA5S3Er X2ZSyBDePW2zRosP0f4ICVWdOKKTv5P6WeXeDAKyynW6pQP5RbjHtPNHHMqaknmbtJ3x sQTg/7apq+tuvjwrPpAeJeVciUcaxcEWsZ/mp2PdZ9y5bqEU2S0Ifg/E906OJhYY1sX3 gErfoKlOJMKAeFFqUkn6Flv0BAGgCP9+HIiEc3su+SiOWXEdar5+cGJhy7jGbCJWERgB ohiw== MIME-Version: 1.0 X-Received: by 10.58.160.10 with SMTP id xg10mr2620774veb.0.1404376609376; Thu, 03 Jul 2014 01:36:49 -0700 (PDT) In-Reply-To: <55f74a23-95ea-4be1-950d-e57e645dab1a@googlegroups.com> References: <55f74a23-95ea-4be1-950d-e57e645dab1a@googlegroups.com> Date: Thu, 3 Jul 2014 18:36:49 +1000 Subject: Re: TypeError expected in an augmented assignment From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 1404376617 news.xs4all.nl 2897 [2001:888:2000:d::a6]:43619 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73887 On Thu, Jul 3, 2014 at 5:51 PM, candide wrote: > Good and interesting observation. But I can't find out where this feature is referenced in the Language/Library Reference. Because, as my first post explains, augmented assignment performs the binary operation associated to the augmented assignment, cf. > > https://docs.python.org/3.2/reference/simple_stmts.html#augmented-assignment-statements > > so seq+= {5, 6} performs seq + {5, 6}, the later raising a TypeError. >From that link: """ An augmented assignment expression like x += 1 can be rewritten as x = x + 1 to achieve a similar, but not exactly equal effect. In the augmented version, x is only evaluated once. Also, 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. """ The significance here is that the augmented assignment may not necessarily be at all comparable to the non-augmented version, but ought to have *approximately* the same *intention*. There are plenty of situations where the two will differ, eg when there are multiple references to the same object: >>> a = b = [1,2] >>> a += [3] >>> a,b ([1, 2, 3], [1, 2, 3]) >>> a = a + [4] >>> a,b ([1, 2, 3, 4], [1, 2, 3]) In its simplest form, x += 1 <-> x = x + 1, but there are plenty of ways to distinguish them. ChrisA