Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed3a.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'element': 0.07; 'assigning': 0.09; 'booth': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'helps.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subclass': 0.16; 'throw': 0.16; 'tuple': 0.16; 'typeerror:': 0.16; 'exception': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; 'chris': 0.29; 'raise': 0.29; "i'm": 0.30; '"",': 0.31; 'index,': 0.31; 'tuples': 0.31; 'file': 0.32; 'class': 0.32; '(most': 0.33; 'fri,': 0.33; 'skip:_ 10': 0.34; 'there': 0.35; 'implement': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'how': 0.40; 'easy': 0.60; 'new': 0.61; 'here': 0.66; 'it!': 0.67; 'mar': 0.68; '257': 0.84; "it'd": 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Tuples and immutability Date: Fri, 07 Mar 2014 12:38:07 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bdb4a7.dip0.t-ipconnect.de User-Agent: KNode/4.11.5 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394192303 news.xs4all.nl 2941 [2001:888:2000:d::a6]:50768 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67992 Chris Angelico wrote: > On Fri, Mar 7, 2014 at 8:33 PM, Duncan Booth > wrote: >> Is there any reason why tuples need to throw an exception on assigning to >> the element if the old value and new value are the same object? > > It'd be easy enough to implement your own tuple subclass that behaves > that way. Try it! See how many situations it actually helps. >>> class T(tuple): ... def __setitem__(self, index, value): ... if value is not self[index]: ... raise TypeError("{} is not {}".format(value, self[index])) ... >>> for i, k in zip(range(250, 260), range(250, 260)): ... T([i])[0] = k ... Traceback (most recent call last): File "", line 2, in File "", line 4, in __setitem__ TypeError: 257 is not 257 I'm not sure "help" is the right word here ;)