Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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; 'else:': 0.03; 'example:': 0.03; 'debug': 0.03; 'situation.': 0.05; 'currently,': 0.07; 'python': 0.08; '(it': 0.09; 'from:addr:python': 0.09; 'tuple': 0.09; 'tuple.': 0.09; 'exception': 0.12; 'this:': 0.15; '"if"': 0.16; '(note': 0.16; 'assignment.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'ignores': 0.16; 'maybe,': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'mysterious': 0.16; 'non-built- in': 0.16; 'predict': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'reply- to:addr:python-list': 0.16; 'rhs': 0.16; 'subject:copy': 0.16; 'tuple,': 0.16; 'wrote:': 0.16; '(i.e.': 0.17; 'header:In-Reply- To:1': 0.22; 'values.': 0.23; 'writes:': 0.25; "i'm": 0.27; 'raise': 0.28; 'effect': 0.28; 'needed,': 0.28; 'anyway.': 0.28; 'convention': 0.28; 'augmented': 0.30; 'object.': 0.30; 'proposing': 0.30; 'actually': 0.32; 'does': 0.32; 'list': 0.32; 'it.': 0.33; 'header:User-Agent:1': 0.33; 'to:addr:python-list': 0.33; 'object': 0.33; 'there': 0.33; 'received:84': 0.34; 'assignment': 0.34; 'reply-to:addr:python.org': 0.34; 'something': 0.35; 'however,': 0.35; 'executing': 0.36; 'reference': 0.37; 'but': 0.37; 'could': 0.37; 'steven': 0.38; 'should': 0.38; 'returned': 0.38; 'except': 0.39; 'called': 0.39; 'subject:: ': 0.39; 'change': 0.40; 'to:addr:python.org': 0.40; 'type': 0.60; 'harder': 0.64; 'believe': 0.65; 'alternative': 0.65; 'exact': 0.68; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; 'succeed': 0.73; 'hand,': 0.76; 'reference?': 0.84; 'subject:write': 0.84; 'examined': 0.91; 'besides,': 0.93 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=Xb8LPfF5 c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=FFsdPcU1_MIA:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=kZ7UWmmPAAAA:8 a=Ro4_AN9BfXyh2GIZz2kA:9 a=ettLjiK3I3-kRZCAxbwA:7 a=wPNLvfGTeEIA:10 a=pyH5b1fOeEsA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Thu, 02 Feb 2012 16:28:28 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:9.0) Gecko/20111222 Thunderbird/9.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: copy on write References: <4f101f45$0$29999$c3e8da3$5496439d@news.astraweb.com> <4f102bd0$0$29999$c3e8da3$5496439d@news.astraweb.com> <4F107AAF.5000600@stoneleaf.us> <20120202141812.649c31d832bb15bd899eb952@johnohagan.com> <4f2a5478$0$29895$c3e8da3$5496439d@news.astraweb.com> <87haz9o6a9.fsf@xemacs.org> In-Reply-To: <87haz9o6a9.fsf@xemacs.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org 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: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328200096 news.xs4all.nl 6969 [2001:888:2000:d::a6]:54496 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19797 On 02/02/2012 10:53, Hrvoje Niksic wrote: > Steven D'Aprano writes: > >> Perhaps you are thinking that Python could determine ahead of time >> whether x[1] += y involved a list or a tuple, and not perform the >> finally assignment if x was a tuple. Well, maybe, but such an approach >> (if possible!) is fraught with danger and mysterious errors even >> harder to debug than the current situation. And besides, what should >> Python do about non-built-in types? There is no way in general to >> predict whether x[1] = something will succeed except to actually try >> it. > > An alternative approach is to simply not perform the final assignment if > the in-place method is available on the contained object. No prediction > is needed to do it, because the contained object has to be examined > anyway. No prediction is needed, just don't. Currently, > lhs[ind] += rhs is implemented like this: > > item = lhs[ind] > if hasattr(item, '__iadd__'): > lhs.__setitem__(ind, item.__iadd__(rhs)) > else: > lhs.__setitem__(ind, item + rhs) > # (Note item assignment in both "if" branches.) > > It could, however, be implemented like this: > > item = lhs[ind] > if hasattr(item, '__iadd__'): > item += rhs # no assignment, item supports in-place change > else: > lhs.__setitem__(ind, lhs[ind] + rhs) > > This would raise the exact same exception in the tuple case, but without > executing the in-place assignment. On the other hand, some_list[ind] += 1 > would continue working exactly the same as it does now. > > In the same vein, in-place methods should not have a return value > (i.e. they should return None), as per Python convention that functions > called for side effect don't return values. > > The alternative behavior is unfortunately not backward-compatible (it > ignores the return value of augmented methods), so I'm not seriously > proposing it, but I believe it would have been a better implementation > of augmented assignments than the current one. > [snip] Could it not perform the assignment if the reference returned by __iadd__ is the same as the current reference? For example: t[0] += x would do: r = t[0].__iadd__(x) if t[0] is not r: t[0] = r Should failed assignment be raising TypeError? Is it really a type error?