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


Groups > comp.lang.python > #69757

Re: Mutable objects inside tuples - good or bad?

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.016
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'python.': 0.02; 'interpreter': 0.05; 'though:': 0.07; 'contexts': 0.09; 'latter': 0.09; 'objects,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'ahead,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hashable': 0.16; 'hashable,': 0.16; 'immutable,': 0.16; 'mutability': 0.16; 'mutable': 0.16; 'objects.': 0.16; 'tuple': 0.16; 'typeerror:': 0.16; 'unhashable': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'variable': 0.18; '>>>': 0.22; 'programming': 0.22; 'putting': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; "i've": 0.25; 'define': 0.26; 'header:In- Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; '"",': 0.31; '>>>>': 0.31; 'agreed.': 0.31; 'consisting': 0.31; 'equality': 0.31; 'gary': 0.31; 'ok.': 0.31; 'tuples': 0.31; 'file': 0.32; 'another': 0.32; '(most': 0.33; 'not.': 0.33; 'totally': 0.33; 'could': 0.34; "can't": 0.35; 'common': 0.35; 'problem.': 0.35; 'definition': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'false': 0.36; 'done': 0.36; 'subject:?': 0.36; 'two': 0.37; 'easily': 0.37; 'problems': 0.38; 'question,': 0.38; 'pm,': 0.38; 'recent': 0.39; 'even': 0.60; 'break': 0.61; 'no.': 0.61; 'john': 0.61; "you're": 0.61; 'back': 0.62; "you'll": 0.62; 'kind': 0.63; 'such': 0.63; 'choose': 0.64; 'here': 0.66; 'believe': 0.68; 'frequently': 0.68; 'useful.': 0.68; 'caused': 0.69; 'legal': 0.71; 'compare:': 0.84; 'confusion.': 0.84; 'fragment': 0.84; 'subject:good': 0.84; 'problems?': 0.91; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=iXQ8I5tCpktxrHmQGgucaWYCgzq1mqeKg4CWcqlxRGI=; b=IQwq0SaAleTznq33J9ayPd/Z7blfLoPT48KXhrYYzs3HiCrAZbitahDlxZtdX7JFlT Hm9yR+6rHukgYFTsvfGe46xV5j5uz7gBabNY/X9LV1gWRpLNSNVMWFNRGhU79ARqc0PI KydzmQnsBURvWmqlLxhli1afIHuWSpz5RxnvBXhKDeFzwv/zkO2Ujqoqsak61CWFMguA Z5wEf5OVNE/8WYLv0Ig0SvuxxioVG3502KFy+DT76w/RjQir7lHDidsTKTJbSyoRdE9l qWNOqD4sG+YpEBHPAMlVkMSrrfHOMJDpyA9NlGYZz5lOjNRh4WtLRJ4GxXW7rXjNQBFV Pi0w==
MIME-Version 1.0
X-Received by 10.68.218.3 with SMTP id pc3mr24537946pbc.71.1396772737030; Sun, 06 Apr 2014 01:25:37 -0700 (PDT)
In-Reply-To <CABicbJK=Fw+m9pjgm9Ng=h=if38YL6uo4ibgiA43S6ohV9XUKg@mail.gmail.com>
References <89df32f9-c8ae-4b7b-bfc4-01c574aabcae@googlegroups.com> <53410185.6050304@islandtraining.com> <CABicbJK=Fw+m9pjgm9Ng=h=if38YL6uo4ibgiA43S6ohV9XUKg@mail.gmail.com>
Date Sun, 6 Apr 2014 18:25:36 +1000
Subject Re: Mutable objects inside tuples - good or bad?
From Chris Angelico <rosuav@gmail.com>
Cc "comp.lang.python" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.8944.1396772745.18130.python-list@python.org> (permalink)
Lines 60
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1396772745 news.xs4all.nl 2854 [2001:888:2000:d::a6]:45826
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:69757

Show key headers only | View raw


On Sun, Apr 6, 2014 at 5:55 PM, Devin Jeanpierre <jeanpierreda@gmail.com> wrote:
> On Sun, Apr 6, 2014 at 12:25 AM, Gary Herron
> <gary.herron@islandtraining.com> wrote:
>> On 04/05/2014 11:53 PM, John Ladasky wrote:
>>>
>>> I find this programming pattern to be useful... but can it cause problems?
>>
>> No.
>>
>> What kind of problems are you considering?  It won't break Python. It's
>> perfectly legal code.
>
> Agreed. Putting mutable objects inside tuples is common and totally OK.

There are many programming habits that can cause problems, even though
they won't break Python and are legal code. :)

>> The tuple c is still immutable, consisting of two specific objects, and (as
>> always) without regard to the specifics or contents of those two objects.
>
> You can choose to define mutability that way, but in many contexts
> you'll find that definition not very useful.
>
> c is such that you could have another variable d, where the following
> interpreter session fragment is easily possible:
>
>>>> c == d
> True
>>>> foo(c)
>>>> c == d
> False

What you're looking at here is hashability, not mutability. Compare:

>>> a = (1,2,3)
>>> hash(a)
2528502973977326415
>>> b = ([1],[2],[3])
>>> hash(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

Both tuples are immutable, but the former is hashable because all its
members are hashable, while the latter is not. You can't trust that
equality with b is constant:

>>> c = ([1],[2],[3])
>>> b == c
True
>>> b[2][0]=4
>>> b == c
False

Going back to the original question, though: I do not believe that
putting mutable objects inside tuples is a problem. I've done it
frequently myself, and it's never caused confusion. So go right ahead,
do it if it makes sense!

ChrisA

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


Thread

Mutable objects inside tuples - good or bad? John Ladasky <john_ladasky@sbcglobal.net> - 2014-04-05 23:53 -0700
  Re: Mutable objects inside tuples - good or bad? Gary Herron <gary.herron@islandtraining.com> - 2014-04-06 00:25 -0700
  Re: Mutable objects inside tuples - good or bad? Devin Jeanpierre <jeanpierreda@gmail.com> - 2014-04-06 00:55 -0700
    Re: Mutable objects inside tuples - good or bad? Rustom Mody <rustompmody@gmail.com> - 2014-04-06 08:07 -0700
  Re: Mutable objects inside tuples - good or bad? Chris Angelico <rosuav@gmail.com> - 2014-04-06 18:25 +1000
  Re: Mutable objects inside tuples - good or bad? Devin Jeanpierre <jeanpierreda@gmail.com> - 2014-04-06 04:01 -0700
  Re: Mutable objects inside tuples - good or bad? Paul Kölle <paul@subsignal.org> - 2014-04-07 17:26 +0200
  Re: Mutable objects inside tuples - good or bad? Chris Angelico <rosuav@gmail.com> - 2014-04-08 01:44 +1000
  Re: Mutable objects inside tuples - good or bad? Paul Kölle <paul@subsignal.org> - 2014-04-07 21:46 +0200
  Re: Mutable objects inside tuples - good or bad? Chris Angelico <rosuav@gmail.com> - 2014-04-08 08:47 +1000
  Re: Mutable objects inside tuples - good or bad? Terry Reedy <tjreedy@udel.edu> - 2014-04-07 20:16 -0400
    Re: Mutable objects inside tuples - good or bad? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-04-08 02:53 +0000

csiph-web