Path: csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.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; 'operator': 0.03; '(even': 0.05; 'assignment': 0.07; 'tries': 0.07; '#error': 0.09; 'already.': 0.09; 'exception,': 0.09; 'modifies': 0.09; 'name?': 0.09; 'raises': 0.09; 'try:': 0.09; '#no': 0.16; 'changed)': 0.16; 'tuple': 0.16; 'typeerror:': 0.16; 'why,': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; "hasn't": 0.19; 'result.': 0.19; 'thu,': 0.19; 'feb': 0.22; '>>>': 0.22; 'creating': 0.23; 'error': 0.23; 'replace': 0.24; '+0200': 0.26; 'header:In-Reply-To:1': 0.27; '[1]': 0.29; '"",': 0.31; '>>>>': 0.31; 'object.': 0.31; 'slot': 0.31; 'file': 0.32; '(most': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'object,': 0.36; 'done': 0.36; 'charset:us-ascii': 0.36; 'list': 0.37; 'step': 0.37; 'lists.': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'then,': 0.60; 'new': 0.61; 'john': 0.61; 'name': 0.63; 'received:122': 0.63; 'different': 0.65 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:date:from:to:subject:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=iUY6EzpATSG4zqIJzv5wD3EGI1VdB96agTh4KE58sSs=; b=CV1j3+5RMW6xqFr1C3XPMTzDcmVRHlK0LfMm0vnqy6ptUXFdEJ2YKOOKN5+IyZ9pEQ UsEgffSBM0cZF6iJgN34z1mxdd2j3z1/9Tnnmj0j7r5JY4kwYXzr90Uf7HrABSFS3eRe +xqszooFwM6dB832CMA6UujYuwxxQz8HGrY9gGFRTZ6Gy70mQwyWVY+ZEvOe+ZAkY6fv ZQvb4cWXPCPit3F6xPsXyU7DJve2KVJPvHS7q6vQgESNkMwquD9uE8Ec62aIA+MnkhKX fUD67AhkJrk9kwyi69HSoNv+moDLcgS9f4M+r7BkiY9sV8b8iPIw/2jF6wZt3qQgmNFi KHvw== X-Received: by 10.66.240.4 with SMTP id vw4mr1303911pac.26.1393564637642; Thu, 27 Feb 2014 21:17:17 -0800 (PST) Sender: "John O'Hagan" Date: Fri, 28 Feb 2014 16:17:10 +1100 From: John O'Hagan To: python-list@python.org Subject: Re: Tuples and immutability In-Reply-To: <87a9dczfya.fsf@elektro.pacujo.net> References: <87a9dczfya.fsf@elektro.pacujo.net> X-Mailer: Claws Mail 3.9.3 (GTK+ 2.24.22; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit 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: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393564641 news.xs4all.nl 2914 [2001:888:2000:d::a6]:37790 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67197 On Thu, 27 Feb 2014 18:19:09 +0200 Marko Rauhamaa wrote: > Eric Jacoboni : > > >>>> a_tuple[1] += [20] > > Traceback (most recent call last): > > File "", line 1, in > > TypeError: 'tuple' object does not support item assignment > > > > [...] > > > > But, then, why a_tuple is still modified? > > That's because the += operator > > 1. modifies the list object in place > > 2. tries to replace the tuple slot with the list (even though the > list hasn't changed) > > It's Step 2 that raises the exception, but the damage has been done > already. > > One may ask why the += operator modifies the list instead of creating > a new object. That's just how it is with lists. > > BTW, try: > > >>> a_tuple[1].append(20) > >>> a_tuple[1].extend([20]) > > Try also: > > >>> a_tuple[1] = a_tuple[1] > [...] Also try: x = a_tuple[1] #What's in a name? x is a_tuple[1] #Obviously, but: x += [1] #No error a_tuple[1] += [1] #Error Same object, just a different name - but a different result. I get why, but still find that odd. -- John