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


Groups > comp.lang.python > #67291

Re: Tuples and immutability

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'that?': 0.05; 'assignment': 0.07; '[1,': 0.09; 'classes.': 0.09; 'considered,': 0.09; 'immutable': 0.09; 'python': 0.11; '(0,': 0.16; '3],': 0.16; 'fails.': 0.16; 'fine.': 0.16; 'immutability': 0.16; 'immutable,': 0.16; 'mutable': 0.16; 'tup': 0.16; 'tuple': 0.16; 'workaround:': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'basically': 0.19; 'solution.': 0.20; 'feb': 0.22; 'putting': 0.22; 'error': 0.23; 'propose': 0.24; 'earlier': 0.24; 'header:In-Reply-To:1': 0.27; 'specifically': 0.29; 'generally': 0.29; 'raise': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'fixing': 0.31; 'index,': 0.31; 'tuples': 0.31; 'types.': 0.31; "we're": 0.32; 'entirely': 0.33; 'fri,': 0.33; 'actual': 0.34; 'could': 0.34; "can't": 0.35; 'knows': 0.35; 'problem.': 0.35; 'objects': 0.35; 'one,': 0.35; 'usual': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'acceptable': 0.36; 'instances': 0.36; 'words,': 0.36; 'doing': 0.36; 'possible': 0.36; 'should': 0.36; 'changing': 0.37; 'list': 0.37; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'changed': 0.39; 'how': 0.40; 'even': 0.60; 'simple': 0.61; 'first': 0.61; 'talking': 0.65; 'side': 0.67; 'containing': 0.69; 'preventing': 0.84; 'wanting': 0.93
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:content-transfer-encoding; bh=K+RwTpd7cdFJXa6l73zm+ZDwtrLf3lfwOM3zaF2ndQw=; b=bLT5fCqHDonKJ+lJDWmSI0JWEKxUAlS32tuC5+8G8baJskr7q+ERxSmmv46Q6Gxgtl yrUi4Anl7SZO79Fs6ph7Q6J/I1CoVtPV1sxnO/a50yT5bW7L5ha4EOpdnkMgM1iSY1Qp A8sje+ojX7plvM+XG404mZbtfdki/W68v99mj02xtJ6mZDJHqp9bOQEwRIloKWsl3Lcc 9chCCZbs5uJPqj0ppL0a2sY7jXqRQ7wF9fHNGQWAPdJLpQNdmrLYGCfxTc3KYj2NMli6 wfOKmCqDWmDRsq4ALgcWfDAOzO3N5qoFrlPTnmclI6lc4glG9X37zNGFZkk4vSLMn5OF +lww==
X-Received by 10.68.129.201 with SMTP id ny9mr7966799pbb.70.1393651019210; Fri, 28 Feb 2014 21:16:59 -0800 (PST)
MIME-Version 1.0
In-Reply-To <059a3d10-453a-40fd-99f9-33ceb8ecabf7@googlegroups.com>
References <lennh4$kpm$1@cabale.usenet-fr.net> <059a3d10-453a-40fd-99f9-33ceb8ecabf7@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 28 Feb 2014 22:16:18 -0700
Subject Re: Tuples and immutability
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
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.7499.1393651028.18130.python-list@python.org> (permalink)
Lines 32
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1393651028 news.xs4all.nl 2886 [2001:888:2000:d::a6]:47231
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:67291

Show key headers only | View raw


On Fri, Feb 28, 2014 at 5:22 PM, Mark H. Harris <harrismh777@gmail.com> wrote:
> I really think this is a bug; honestly.  IMHO it should be an error to use  +=  with an immutable type and that means not at all.  In other words,  the list should not even be considered, because we're talking about changing a tuple... which should not be changed (nor should its members be changed).

How would you propose doing that?  Bear in mind that while Python
knows that tuples specifically are immutable, it doesn't generally
know whether a type is immutable.  The best it can do is try the
operation and raise a TypeError if it fails.  So you can special-case
the code for += to fail earlier if the left side of the assignment is
an tuple index, but it's not currently possible to do the same for
arbitrary immutable user types.

Even if that is solved, how you do propose preventing the simple workaround:
    tup = (0, [1, 2, 3], 4)
    thelist = tup[1]
    thelist += [5, 6]
which currently works just fine.

I don't think that entirely disallowing mutable objects inside tuples
is an acceptable solution.  For one, it would mean never allowing
instances of custom classes.  They could then basically only contain
strings, numbers, frozensets, and other tuples.  For another, my
experience suggests that if I'm putting a list inside a tuple in the
first place, I am generally not relying on the immutability of that
tuple anyway, so I don't really see this as fixing an actual problem.
The usual reason for wanting an immutable object is to hash it, and a
tuple containing a list already can't be hashed.

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