Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed3a.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'value,': 0.04; 'abuse': 0.07; 'assignment': 0.07; 'attribute': 0.07; 'indexing': 0.07; 'none,': 0.07; '[1,': 0.09; 'assumed': 0.09; 'augmented': 0.09; 'boundaries': 0.09; 'handful': 0.09; 'hooks': 0.09; 'immutable': 0.09; 'implements': 0.09; 'operand': 0.09; 'raises': 0.09; 'python': 0.11; 'assume': 0.14; 'itself.': 0.14; 'arbitrarily': 0.16; 'concatenate': 0.16; 'deque': 0.16; 'deque,': 0.16; 'did.': 0.16; 'empty,': 0.16; 'exactly,': 0.16; 'in-place': 0.16; 'iterators': 0.16; 'length.': 0.16; 'literals,': 0.16; 'mutable': 0.16; 'operation,': 0.16; 'overridden': 0.16; 'permissive': 0.16; 'programmer,': 0.16; 'shorthand': 0.16; 'str,': 0.16; 'tuple': 0.16; 'twisted': 0.16; 'typeerror:': 0.16; 'exception': 0.16; 'all.': 0.16; 'language': 0.16; 'wrote:': 0.18; '(not': 0.18; 'library': 0.18; 'wed,': 0.18; 'addition,': 0.20; 'seems': 0.21; '>>>': 0.22; 'equivalent': 0.26; 'nearly': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'appear': 0.29; 'raise': 0.29; 'sets': 0.30; 'especially': 0.30; 'message- id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; '"",': 0.31; "d'aprano": 0.31; 'enforce': 0.31; 'fast.': 0.31; 'int,': 0.31; 'safely': 0.31; 'shoot': 0.31; 'steven': 0.31; 'file': 0.32; 'skip:c 30': 0.32; '(e.g.': 0.33; '(most': 0.33; 'classes': 0.35; 'knows': 0.35; 'beyond': 0.35; 'operations': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'consistent': 0.36; 'curious': 0.36; 'sequence': 0.36; 'should': 0.36; 'list': 0.37; 'implement': 0.38; 'thank': 0.38; 'mapping': 0.38; 'to:addr :python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'anything': 0.39; 'recent': 0.39; 'does': 0.39; '12,': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'ian': 0.60; 'strictly': 0.61; "you've": 0.63; 'more': 0.64; 'mar': 0.68; 'anything.': 0.68; 'stated': 0.69; 'designers': 0.74; 'viewed': 0.74; 'yourself': 0.78; 'different.': 0.84; 'everything,': 0.84; 'irrelevant': 0.84; 'type(s)': 0.84; 'you;': 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=YG+wCIZUC+NSk3bF2u0xAw4pmUEDQZ6nMxDxYB5N1gw=; b=Ar4raMqWk35TgZmUKDqPuMpOd/SKphSVEkptrdnXGI+pvWEeUrmOis/rZJYrh9ITUo 4Jy+RHJe2tAOaL8w2UzO8em+tijJxny0GGjwndGT30blRRHyWXNS09CK1s+yBAm9vHuI Zj97MCSayRqgR5nF7yrFh+uP92f9ONr+EdthJv2CDu4bog3aOYc7qPan6gTl/Bwmvftr P4VBQLG9190gr7OUz+BUXSyhDithJfbVo/OmSzz8mgVQkGSWhn+lLgglhbn06DM0LzVZ Ae46LyJODkomfpEP3BddxR7vt07S/57QNtwfafYRemVKVh2ErrgFA3gHMQLo3S9FKiDh uMEA== X-Received: by 10.66.146.105 with SMTP id tb9mr711361pab.157.1394674570087; Wed, 12 Mar 2014 18:36:10 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <5320ebce$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <531f3dfb$0$29994$c3e8da3$5496439d@news.astraweb.com> <5320ebce$0$29994$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Wed, 12 Mar 2014 19:35:29 -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: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394674579 news.xs4all.nl 2838 [2001:888:2000:d::a6]:44155 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68318 On Wed, Mar 12, 2014 at 5:20 PM, Steven D'Aprano wrote: > On Tue, 11 Mar 2014 17:06:43 -0600, Ian Kelly wrote: > >> That's true but irrelevant to my point, which was to counter the >> assertion that mutable types can always be assumed to be able to perform >> operations in-place. > > "Always"? Not so fast. > > This is Python. We have freedom to abuse nearly everything, and if you > want to shoot yourself in the foot, you can. With the exception of a > handful of things which cannot be overridden (e.g. None, numeric > literals, syntax) you cannot strictly assume anything about anything. > Python does not enforce that iterators raise StopIteration when empty, or > that indexing beyond the boundaries of a sequence raises IndexError, or > that __setitem__ of a mapping sets the key and value, or that __len__ > returns a length. Thank you; you've stated my point more succinctly than I did. > Augmented assignment is no different. The docs describe the intention of > the designers and the behaviour of the classes that they control, so with > standard built-in classes like int, str, list, tuple etc. you can safely > assume that mutable types will perform the operation in place and > immutable types won't, but with arbitrary types from some arbitrarily > eccentric or twisted programmer, who knows what it will do? This got me curious about how consistent the standard library is about this exactly, so I did some grepping. In the standard library there are 5 mutable types that support concatenation that I was able to find: list, deque, array, bytearray, and Counter. There are none that support addition, which I find interesting in that the language provides hooks for in-place addition but never uses them itself. All of the classes above appear to follow the rule that if you can concatenate an operand, you can in-place concatenate the same operand. The converse however does not hold: list.__iadd__ and Counter.__iadd__ are both more permissive in what types they will accept than their __add__ counterparts, and especially interesting to me is that deque implements __iadd__ but does not implement __add__ at all. This last in particular seems to support the assertion that += should be viewed more as a shorthand for an in-place operation, less as an equivalent for x = x + y. >>> l = [1,2,3] >>> l + (4,5,6) Traceback (most recent call last): File "", line 1, in TypeError: can only concatenate list (not "tuple") to list >>> l += (4,5,6) >>> l [1, 2, 3, 4, 5, 6] >>> c = collections.Counter('mississippi') >>> c + collections.Counter('alabama') Counter({'s': 4, 'a': 4, 'i': 4, 'p': 2, 'm': 2, 'b': 1, 'l': 1}) >>> c + dict({'a': 4, 'l': 1, 'b': 1, 'm': 1}) Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'Counter' and 'dict' >>> c += dict({'a': 4, 'l': 1, 'b': 1, 'm': 1}) >>> c Counter({'s': 4, 'a': 4, 'i': 4, 'p': 2, 'm': 2, 'b': 1, 'l': 1}) >>> d = collections.deque([1,2,3]) >>> d += [4,5,6] >>> d deque([1, 2, 3, 4, 5, 6]) >>> d + [7,8,9] Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'collections.deque' and 'list' >>> d.__add__ Traceback (most recent call last): File "", line 1, in AttributeError: 'collections.deque' object has no attribute '__add__'