Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed2.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'operator': 0.03; 'assignment': 0.07; 'modified': 0.07; 'skip:` 10': 0.07; '[1,': 0.09; 'cc:addr:python-list': 0.11; '(1,': 0.16; 'concatenate': 0.16; 'defined.': 0.16; 'in-place': 0.16; 'iterable': 0.16; 'mutable': 0.16; 'nick': 0.16; 'numpy': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; '(not': 0.18; 'thu,': 0.19; 'seems': 0.21; 'feb': 0.22; '>>>': 0.22; 'import': 0.22; '(in': 0.22; 'cc:addr:gmail.com': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**1': 0.23; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; '"",': 0.31; 'consequence': 0.31; 'file': 0.32; '(most': 0.33; 'sense': 0.34; "can't": 0.35; 'operations': 0.35; 'point.': 0.35; 'but': 0.35; 'received:google.com': 0.35; '14,': 0.36; 'list': 0.37; 'recent': 0.39; 'does': 0.39; '12,': 0.39; 'changed': 0.39; 'called': 0.40; 'skip:n 10': 0.64; 'situation': 0.65; 'to:addr:gmail.com': 0.65; 'apart': 0.72; 'obvious': 0.74; 'abuse.': 0.84; 'oscar': 0.84; 'these.': 0.91 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=qxLos8L8CFcJCDU2dxXG1jACGqk1h6Nq/XQN6n/egk0=; b=afCpBIpBsitKJJUZbVO0z4zRWDI7kL3+RgNm/pt8a+gAS6AjQFVc/VMFMpe81MyXoo rQY/KKl6sppxM3KVKsbCK1SbmBxDB5ZF3LMxyPm+4T77FNNt9uaSx03y/D9vPHIlLJuu rNKrKDKMNo0I+r/ubGvc6ANSzTYaCcslo3jlYcMLQwBBT/7TCs1FNdePF0Ex8086fEHF 0LSaHEEVeeNG41Y8DpTachQ621AEnyV/pL4WpqoIHvG6BsBfaZLJ9h0aR8mKKWr5HHN+ SdQM9xImmA2BolWpfQq0XTQw+D4/FoaIMOWSy16spPyq1WeKy6GpzMUr/u1JM59Mx/LC EmRQ== X-Received: by 10.221.22.71 with SMTP id qv7mr3527482vcb.34.1393700173845; Sat, 01 Mar 2014 10:56:13 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: From: Oscar Benjamin Date: Sat, 1 Mar 2014 18:55:53 +0000 Subject: Re: Tuples and immutability To: Nick Timkovich Content-Type: text/plain; charset=ISO-8859-1 Cc: "python-list@python.org" 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393700182 news.xs4all.nl 2939 [2001:888:2000:d::a6]:37216 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67338 On 27 February 2014 21:47, Nick Timkovich wrote: > On Thu, Feb 27, 2014 at 10:33 AM, Chris Angelico wrote: >> >> It's unintuitive, but it's a consequence of the way += is defined. If >> you don't want assignment, don't use assignment :) > > Where is `.__iadd__()` called outside of `list += X`? Numpy uses it for in-place operations with numpy arrays: >>> import numpy >>> a = numpy.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> a[::2] += 10 >>> a array([10, 1, 12, 3, 14, 5, 16, 7, 18, 9]) It makes sense for any mutable type that supports +. The stdlib doesn't have many of these. The obvious one is list but there's also MutableString (in 2.x): >>> from UserString import MutableString >>> a = MutableString("qwe") >>> a 'qwe' >>> b = a >>> b += "asd" >>> a 'qweasd' > If the only difference from `.extend()` is that it returns `self`, but the list was > already modified anyway, why bother with reassignment? I don't know of any situation where __iadd__ is defined but doesn't return self and I can't think of a use case apart from operator abuse. I had thought that the other difference was that .extend would accept any iterable but it seems += does also. Perhaps that was changed at some point. >>> l = [1, 2, 3] >>> l + (1, 2, 3) Traceback (most recent call last): File "", line 1, in TypeError: can only concatenate list (not "tuple") to list >>> l += (1, 2, 3) >>> l [1, 2, 3, 1, 2, 3] Oscar