Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed2.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'retrieved': 0.05; 'root': 0.05; 'tree': 0.05; 'binary': 0.07; 'method.': 0.07; 'modifying': 0.07; '40,': 0.09; 'computed': 0.09; 'exception.': 0.09; 'insertion': 0.09; 'method,': 0.09; 'performs': 0.09; 'def': 0.12; 'itself.': 0.14; 'discarded.': 0.16; 'exception?': 0.16; 'idea:': 0.16; 'in-place': 0.16; 'in- place,': 0.16; 'lhs': 0.16; 'thread,': 0.16; 'modification': 0.16; 'wrote:': 0.18; 'balancing': 0.24; 'earlier': 0.24; 'header:In- Reply-To:1': 0.27; 'raise': 0.29; "doesn't": 0.30; 'returned': 0.30; 'message-id:@mail.gmail.com': 0.30; 'node': 0.31; 'class': 0.32; 'another': 0.32; 'fri,': 0.33; 'skip:_ 10': 0.34; 'maybe': 0.34; 'equal': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'object,': 0.36; 'raising': 0.36; 'method': 0.36; 'should': 0.36; 'being': 0.38; 'implement': 0.38; 'filter': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'itself': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'mentioned': 0.61; 'simply': 0.61; 'different': 0.65; '30,': 0.65; 'mar': 0.68; '20,': 0.68; '*and*': 0.84; 'balanced': 0.84; 'stored,': 0.84 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; bh=4iOkYAWOFefoeO9zeUmgOir8Mu5/UeyLaM+sH2Iokc0=; b=aygGixTyAvBli/CKVOxvxdFM1ocfdE+14FcXyZNsOD1POmMa9lqF9Oc4faIF7kr+S+ 9de97Geg4jtzjrV1n/F/WSgD/ALoqFwZTnNWZFWbkvyfmfdJBqyHmiINS8Maj/dkCo+E mszY5TeJJgpBueCUeIxfID3qU7ewmhpF1US0VxOLDvgUUeai1PIt2GveazGFMjHbh/HE cpf87CBvcE5G07EplubI8Cb6iBp3+D1H/LogOVCFJ82zFhdi090D3j6ghUHCOylgXcVi hW+I+D0mTlQrqBsL79/Xu4+mOu9A8dzI8s3/SptIxJ4P8AgWpNbTBj3vaqbBB6NJCubH PhfQ== X-Received: by 10.68.129.201 with SMTP id ny9mr26632535pbb.70.1394252275180; Fri, 07 Mar 2014 20:17:55 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Fri, 7 Mar 2014 21:17:14 -0700 Subject: Re: Tuples and immutability To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394252278 news.xs4all.nl 2901 [2001:888:2000:d::a6]:57702 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68023 On Fri, Mar 7, 2014 at 7:17 PM, Gregory Ewing wrote: > Here's another idea: If the __iadd__ method returns the > same object, *and* the LHS doesn't have a __setitem__ > method, then do nothing instead of raising an exception. Maybe it doesn't have a __setitem__ because the object that was retrieved is computed rather than stored, and the result of the __iadd__ will simply be discarded. Somewhat contrived example: class LessThanFilter: def __init__(self, the_list): self._the_list = the_list def __getitem__(self, bound): return [x for x in self._the_list if x < bound] filter = LessThanFilter([10, 20, 30, 40, 50]) filter[25] += [15, 17, 23] Should that last line not raise an exception? The __iadd__ call will return the same object, and the LHS doesn't have a __setitem__ method. > I don't think that's a problem, because the use case > being addressed is where the object performs in-place > modification and always returns itself. Any object that > doesn't return itself is not modifying in-place, even > if the returned object happens to be equal to the > original one. I already mentioned this earlier in the thread, but a balanced binary tree might implement += as node insertion and then return a different object if the balancing causes the root node to change.