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


Groups > comp.lang.python > #8248

Re: what happens inside?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'terry': 0.07; 'cant': 0.09; 'dict': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'tuple': 0.09; 'tuple.': 0.09; '>>>': 0.12; 'am,': 0.14; 'wrote:': 0.14; 'examples.': 0.16; 'hashable': 0.16; 'immutable': 0.16; 'keys.': 0.16; 'methods,': 0.16; 'reedy': 0.16; 'type:': 0.16; 'unhashable': 0.16; 'traceback': 0.16; '(most': 0.16; 'jan': 0.20; 'header:In-Reply-To:1': 0.21; 'last):': 0.23; '(or': 0.24; 'subject:?': 0.29; 'affected': 0.29; 'lists': 0.29; 'do.': 0.30; 'tuples': 0.30; 'typeerror:': 0.30; 'changes': 0.30; 'header:X -Complaints-To:1': 0.32; 'someone': 0.33; 'does': 0.33; 'to:addr :python-list': 0.33; 'list': 0.33; 'file': 0.34; 'there': 0.35; 'header:User-Agent:1': 0.35; '"",': 0.35; 'skip:. 10': 0.36; 'change': 0.37; 'element': 0.37; 'case': 0.37; 'received:org': 0.38; 'but': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'containing': 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'note:': 0.63; '11:45': 0.84; '2],': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: what happens inside?
Date Wed, 22 Jun 2011 19:00:16 -0400
References <BANLkTim5-pusEdT2Qhm-0NJtbazLJMvxXA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host rain.gmane.org
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10
In-Reply-To <BANLkTim5-pusEdT2Qhm-0NJtbazLJMvxXA@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.304.1308783688.1164.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 82.94.164.166
X-Trace 1308783688 news.xs4all.nl 14132 [::ffff:82.94.164.166]:35905
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:8248

Show key headers only | View raw


On 6/22/2011 11:45 AM, Chetan Harjani wrote:
> why tuples are immutable whereas list are mutable?

Because tuples do not have mutation methods, which lists do.
Tuple and lists both have .__getitem__ but tuples do not have 
.__setitem__ or .__delitem__ (or .append, .extend, .sort, or .reverse).

> why when we do x=y where y is a list and then change a element in x, y
> changes too( but the same is not the case when we change the whole value
> in x ), whereas, in tuples when we change x, y is not affected and also
> we cant change each individual element in tuple. Someone please clarify.

For specific answers, you should give specific examples.

(1,2)[0] = 3 does not work because there is no tuple.__setitem__. But note:
 >>> a = ([1,2], 3)
 >>> b = a
 >>> a[0][0] = 4
 >>> b
([4, 2], 3)
Tuples containing mutables are not immutable all the way down.

They are also not hashable (if any element is not hashable) and cannot 
be used as dict keys.
 >>> hash(a)
Traceback (most recent call last):
   File "<pyshell#5>", line 1, in <module>
     hash(a)
TypeError: unhashable type: 'list'

-- 
Terry Jan Reedy

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


Thread

Re: what happens inside? Terry Reedy <tjreedy@udel.edu> - 2011-06-22 19:00 -0400

csiph-web