Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!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; 'interpreter': 0.05; 'preferably': 0.05; 'string.': 0.05; 'true,': 0.05; 'assignment': 0.07; 'class,': 0.07; 'context': 0.07; 'modifying': 0.07; 'string': 0.09; '"if': 0.09; 'augmented': 0.09; 'immutable': 0.09; 'modifies': 0.09; 'python': 0.11; 'creates': 0.14; 'immutability': 0.16; 'immutable,': 0.16; 'implies': 0.16; 'in-place': 0.16; 'in- place,': 0.16; 'it".': 0.16; 'merely': 0.16; 'mutability': 0.16; 'mutable': 0.16; 'operates': 0.16; 'operator.': 0.16; 'opposite': 0.16; 'types,': 0.16; 'exception': 0.16; 'language': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'addition,': 0.20; 'meant': 0.20; '>>>': 0.22; "aren't": 0.24; 'documented': 0.24; 'least': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'message- id:@mail.gmail.com': 0.30; "d'aprano": 0.31; 'steven': 0.31; 'class': 0.32; 'interface': 0.32; 'quite': 0.32; 'says': 0.33; 'not.': 0.33; 'could': 0.34; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'interface,': 0.36; 'returning': 0.36; 'done': 0.36; 'should': 0.36; 'detail': 0.37; 'list': 0.37; 'list.': 0.37; 'somebody': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'even': 0.60; 'ian': 0.60; 'new': 0.61; 'course': 0.61; 'offer': 0.62; 'guarantee': 0.63; 'more': 0.64; 'mar': 0.68; 'obvious': 0.74; 'behavior': 0.77; 'beside': 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=xY4gqAx8n/Uoka9vocGD970WP/CBX0dWmAaS2Y+cbnE=; b=Yz+yBcZqZkvEwugJ/DXKkfcPc1d+2LHMY6W4+GrK+ALb1Wdr9HcW+uHl6jG2O9ZY6I wGg/CSSIhJaWPmHChTzlTg15ib9gpwVIkCVCYTEWCWaZxaS+uBSBMLgi0Ef34hu0Rple VCQmM2j3GEEV+p+alribNNAeiyb79wTmWhOG+1TAe1F+PcX/G5t2Gg/wiNYAPCRBr2jv hd7lXAxFTIvqfcKdH6ug7T5/mY76LGLuDj6DXdqEwSDYy5uedi0RGC1NDNxY5+kityRy Muh3O6WZDXo7e5wpopruGY1iq4dxlsYc1Opsjg63POyqaw1gfki2KJvkOlKgxSTKZVxz lLrg== X-Received: by 10.68.143.100 with SMTP id sd4mr39052420pbb.0.1394440576991; Mon, 10 Mar 2014 01:36:16 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <531d2551$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <531d2551$0$29994$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Mon, 10 Mar 2014 02:35:36 -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: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394440586 news.xs4all.nl 2842 [2001:888:2000:d::a6]:54468 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68127 On Sun, Mar 9, 2014 at 8:37 PM, Steven D'Aprano wrote: > On Sun, 09 Mar 2014 17:42:42 -0600, Ian Kelly wrote: > >> On Sun, Mar 9, 2014 at 4:03 PM, Gregory Ewing >> wrote: > >>> 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. > > That's incorrect. Ints aren't merely "meant" to be immutable, which > implies that's it's optional, they are defined by the language > specification and the reference implementation as immutable. Any > interpreter where ints are mutable *is not Python*. That's true, but is beside the point, which is that "when possible" is not very meaningful. >> 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. > > Whether += operates in place or not is part of the interface of the > class, not the implementation. > > Would you say that whether list.append operates in place or creates a new > list is an implementation detail? Whether str.upper() creates a new > string or modifies the existing one in place? Of course not. list.append is documented as modifying the list. str.upper is documented as returning a copy of the string. > Mutability versus > immutability is part of the interface, not implementation, not > withstanding that somebody could create an alternative class with the > opposite behaviour: a MutableStr, or ImmutableList. If the in-place behavior of += is held to be part of the interface, then we must accept that += is not polymorphic across mutable and immutable types, which in my mind largely* defeats the purpose of having it. After all, there should be one -- and preferably only one -- obvious way to do it. If you want in-place concatenation, the obvious way to do it is by calling extend. If you want copy concatenation, the obvious way to do it is with the + operator. Why then should not just mutable sequences but immutable sequences as well even offer the += operator? * The one exception I can think of is numpy, where there is no more obvious way to do in-place addition, and in that context I would consider the in-place behavior to be part of the interface.