Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.datemas.de!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.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; 'assignment': 0.07; 'modified': 0.07; 'statically': 0.07; 'arrays': 0.09; 'assigning': 0.09; 'augmented': 0.09; 'calculating': 0.09; 'similar,': 0.09; 'target,': 0.09; 'cc:addr:python-list': 0.11; '"an': 0.16; '3],': 0.16; 'cc:name:python list': 0.16; 'effect.': 0.16; 'evaluating': 0.16; 'in-place': 0.16; 'in-place,': 0.16; 'mutable': 0.16; 'nope,': 0.16; 'numpy': 0.16; 'once.': 0.16; 'reedy': 0.16; 'rewritten': 0.16; 'semantically': 0.16; 'thursday,': 0.16; 'wrote:': 0.18; 'possible,': 0.19; 'meant': 0.20; '>>>': 0.22; 'example': 0.22; 'import': 0.22; 'manual': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'instead.': 0.24; 'cc:2**0': 0.24; 'nearly': 0.26; 'possibly': 0.26; 'world,': 0.26; 'header:In-Reply-To:1': 0.27; 'array': 0.29; 'ideal': 0.29; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; 'fast.': 0.31; 'says': 0.33; 'actual': 0.34; 'except': 0.35; 'equal': 0.35; 'johnson': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'should': 0.36; 'two': 0.37; 'version,': 0.38; 'pm,': 0.38; 'rather': 0.38; 'expression': 0.60; 'ian': 0.60; 'new': 0.61; 'march': 0.61; 'more': 0.64; "'similar'": 0.84; 'elementary': 0.84; 'oscar': 0.84; 'same,': 0.91; 'rick': 0.93 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 :cc:content-type; bh=QlPVyFDojoQTeVDfO7nYR66gMwL4aYMS2u2t5UuEyJY=; b=uAaYe2ZiZGOTXvRn95KZ7uGJhmVpnqhZrIwXSLU83ZfhGjSugwuTLEgj6LGsXysWnY IaxywTuNd+FFDowUg4Ai1OWJuDCXEaoOUNbBSZ3FOMgzgTasKK6icdR9JXFgjOuU/duA uVBPCMBs+xt24zcsI3eLWldCz1rEajKpi+yF/O53+cjB0RNylUSOvNH+zup9vH2LWSVi oMWaj66JcxIJLaTNZ2lLYQDCC/ajBQ7j+HJ/UQD7xI6JmCPfr+JJF7HTqcYF9XL4dVZ5 Sha7lMugtDG+Yg0qygx265PGxawT0JI62Fx8GsjH6vG2OI4/Ua0/qQ7PcBkhTX4cgxmK gyvg== X-Received: by 10.220.136.6 with SMTP id p6mr32151876vct.9.1394619645697; Wed, 12 Mar 2014 03:20:45 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <0d76f320-a39e-44e9-85a2-74220b646566@googlegroups.com> From: Oscar Benjamin Date: Wed, 12 Mar 2014 10:20:25 +0000 Subject: Re: Tuples and immutability To: Terry Reedy Content-Type: text/plain; charset=ISO-8859-1 Cc: Python List 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: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394619648 news.xs4all.nl 2829 [2001:888:2000:d::a6]:55334 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68269 On 12 March 2014 03:25, Terry Reedy wrote: > On 3/11/2014 10:01 PM, Rick Johnson wrote: >> >> >> On Thursday, February 27, 2014 4:18:01 PM UTC-6, Ian wrote: >>> >>> x += y is meant to be equivalent, except possibly in-place and >>> more efficient, than x = x + y. > > > The manual actually says "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. > > > >> In an ideal world, the speed of these two codes should be the same, > > > 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. An example where the result is semantically different: >>> from numpy import array >>> a = array([1, 2, 3], dtype=int) >>> a array([1, 2, 3]) >>> a + 1.1 array([ 2.1, 3.1, 4.1]) >>> a += 1.1 >>> a array([2, 3, 4]) The reason is that numpy arrays are both mutable and have statically determined elementary data type: >>> (a + 1.1).dtype dtype('float64') >>> a.dtype dtype('int64') Oscar