Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #67197

Re: Tuples and immutability

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 <johnmohagan@gmail.com>
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" <johnmohagan@gmail.com>
Date Fri, 28 Feb 2014 16:17:10 +1100
From John O'Hagan <research@johnohagan.com>
To python-list@python.org
Subject Re: Tuples and immutability
In-Reply-To <87a9dczfya.fsf@elektro.pacujo.net>
References <lennh4$kpm$1@cabale.usenet-fr.net> <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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.7456.1393564641.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Thu, 27 Feb 2014 18:19:09 +0200
Marko Rauhamaa <marko@pacujo.net> wrote:

> Eric Jacoboni <eric.jacoboni@gmail.com>:
> 
> >>>> a_tuple[1] += [20]
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> > 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



Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Tuples and immutability Eric Jacoboni <eric.jacoboni@gmail.com> - 2014-02-27 17:01 +0100
  Re: Tuples and immutability Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-27 10:13 -0600
    Re: Tuples and immutability Eric Jacoboni <eric.jacoboni@gmail.com> - 2014-02-27 17:27 +0100
      Re: Tuples and immutability Chris Angelico <rosuav@gmail.com> - 2014-02-28 03:33 +1100
      Re: Tuples and immutability Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-27 10:47 -0600
      Re: Tuples and immutability Nick Timkovich <prometheus235@gmail.com> - 2014-02-27 15:47 -0600
      Re: Tuples and immutability Chris Angelico <rosuav@gmail.com> - 2014-02-28 08:52 +1100
      Re: Tuples and immutability Ian Kelly <ian.g.kelly@gmail.com> - 2014-02-27 15:18 -0700
      Re: Tuples and immutability Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-03-01 18:55 +0000
  Re: Tuples and immutability Chris Angelico <rosuav@gmail.com> - 2014-02-28 03:14 +1100
  Re: Tuples and immutability Marko Rauhamaa <marko@pacujo.net> - 2014-02-27 18:19 +0200
    Re: Tuples and immutability John O'Hagan <research@johnohagan.com> - 2014-02-28 16:17 +1100
      Re: Tuples and immutability Marko Rauhamaa <marko@pacujo.net> - 2014-02-28 09:54 +0200
  Re: Tuples and immutability Joshua Landau <joshua@landau.ws> - 2014-02-28 14:41 +0000
  Re: Tuples and immutability Chris Angelico <rosuav@gmail.com> - 2014-03-01 01:43 +1100
  Re: Tuples and immutability "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 16:22 -0800
    Re: Tuples and immutability Eric Jacoboni <eric.jacoboni@gmail.com> - 2014-03-01 02:27 +0100
      Re: Tuples and immutability "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 20:45 -0800
        Re: Tuples and immutability Ian Kelly <ian.g.kelly@gmail.com> - 2014-02-28 22:34 -0700
          Re: Tuples and immutability "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 21:50 -0800
            Re: Tuples and immutability Ned Batchelder <ned@nedbatchelder.com> - 2014-03-01 12:56 -0500
      Re: Tuples and immutability Ian Kelly <ian.g.kelly@gmail.com> - 2014-02-28 22:26 -0700
    Re: Tuples and immutability Chris Angelico <rosuav@gmail.com> - 2014-03-01 12:39 +1100
    Re: Tuples and immutability Ian Kelly <ian.g.kelly@gmail.com> - 2014-02-28 22:16 -0700
      Re: Tuples and immutability "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 22:16 -0800
        Re: Tuples and immutability Chris Angelico <rosuav@gmail.com> - 2014-03-01 17:29 +1100
        Re: Tuples and immutability Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-01 14:54 +0000
          Re: Tuples and immutability "Mark H. Harris" <harrismh777@gmail.com> - 2014-03-01 13:01 -0800
      Re: Tuples and immutability "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 22:25 -0800
        Re: Tuples and immutability Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-01 12:45 -0700
      Re: Tuples and immutability "Mark H. Harris" <harrismh777@gmail.com> - 2014-03-01 13:21 -0800
        Re: Tuples and immutability Eric Jacoboni <eric.jacoboni@gmail.com> - 2014-03-02 03:04 +0100
          Re: Tuples and immutability "Mark H. Harris" <harrismh777@gmail.com> - 2014-03-01 18:28 -0800
          Re: Tuples and immutability Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-02 05:32 -0700
            Re: Tuples and immutability Eric Jacoboni <eric.jacoboni@gmail.com> - 2014-03-02 14:38 +0100
              Re: Tuples and immutability Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-02 14:05 +0000
                Re: Tuples and immutability Eric Jacoboni <eric.jacoboni@gmail.com> - 2014-03-02 15:17 +0100
                Re: Tuples and immutability "albert visser" <albert.visser@gmail.com> - 2014-03-02 15:37 +0100
        Re: Tuples and immutability "Mark H. Harris" <harrismh777@gmail.com> - 2014-03-01 18:15 -0800

csiph-web