Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4a.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'syntax': 0.04; 'explicitly': 0.05; '*not*': 0.07; 'assignment': 0.07; '40,': 0.09; 'method,': 0.09; 'way:': 0.09; 'def': 0.12; 'bug': 0.12; 'wrote': 0.14; "wouldn't": 0.14; 'assignment.': 0.16; 'exception?': 0.16; 'in-place': 0.16; 'in-place,': 0.16; 'lhs': 0.16; 'operator.': 0.16; 'shorthand': 0.16; 'sat,': 0.16; 'language': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'trying': 0.19; 'differ': 0.19; 'not,': 0.20; 'code,': 0.22; 'either.': 0.24; "shouldn't": 0.24; 'equivalent': 0.26; 'second': 0.26; 'header:In- Reply-To:1': 0.27; 'raise': 0.29; 'said,': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'relies': 0.31; 'class': 0.32; 'lists': 0.32; 'probably': 0.32; 'extend': 0.32; "we're": 0.32; 'quite': 0.32; 'bugs': 0.33; 'entirely': 0.33; 'skip:_ 10': 0.34; "can't": 0.35; '(2)': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'being': 0.38; 'filter': 0.38; 'to:addr :python-list': 0.38; 'issue': 0.38; 'pm,': 0.38; 'expect': 0.39; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'read': 0.60; 'catch': 0.60; 'ian': 0.60; "you're": 0.61; 'you.': 0.62; 'guarantee': 0.63; 'such': 0.63; 'different': 0.65; '30,': 0.65; 'mar': 0.68; '20,': 0.68; 'fact,': 0.69; 'guaranteed': 0.75; "class's": 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=sSE8z3zGhh6OQDBZPibjveZtWfZfR9X6SVwvsCKN6aA=; b=nxpb9AtRGtmgjQ7EpHTlfX48Jw9FAzCqzd3nqt4y4wZhBZU1aSI6VVn+yE56MAOplh 8AJrliLvp1+Uw0y/DrvPrCyxRj39419vd24CvYAfRB2R88d8LPZcLwnjafov/DEgWxm8 c0/o+2cZOxYU6kmjodmen9ZYpGFwXrfv5x+12EghqoLLXqLoakOrTfpXlGEByhyeOvyE 5i6l0sqGQNJWvxV7VWTt58FQohyMHT5IxskhS92U3ePhYoqD+jCV1fyCIT/mbC3p0egZ OlmPyGQV2v7bSh6PnKZl6Pl1Y5F+tJBs+XPt9+FzhHSvabJDdlBRtG80Vkzr8nsBpXqP j/Tw== X-Received: by 10.68.191.200 with SMTP id ha8mr31790627pbc.66.1394332830433; Sat, 08 Mar 2014 18:40:30 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Sat, 8 Mar 2014 19:39:50 -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: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394332838 news.xs4all.nl 2855 [2001:888:2000:d::a6]:43032 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68060 On Sat, Mar 8, 2014 at 5:40 PM, Gregory Ewing wrote: > Ian Kelly wrote: >> >> 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? > > > In this case it will fail to catch what is probably an error, > but you can't expect the language to find all your bugs for > you. If you wrote the same bug this way: > > filter[25].extend([15, 17, 23]) > > it wouldn't be caught either. > > What's happening is that we're trying to use the syntax > a += b to mean two different things: > > 1) Shorthand for a = a + b > > 2) A way of expressing an in-place modification, such > as a.extend(b) > > Case (2) is not really an assignment at all, so arguably > it shouldn't require the LHS to support assignment. In my view the second one is wrong. a += b should be understood as being equivalent to a = a + b, but with the *possible* and by no means guaranteed optimization that the operation may be performed in-place. In fact, if you read the documentation for lists, you may notice that while they clearly cover the + operator and the extend method, they do not explicitly document the list class's += operator. So although I'm not entirely sure whether it is intentional or not, and I would be quite surprised if some implementation were actually to differ on this point, the language does *not* from what I can see guarantee that the += operator on lists is equivalent to calling .extend. That having been said, code that uses += and relies on the operation to be performed in-place should be considered buggy. If you need the operation to be performed in-place, then use in-place methods like list.extend. If you need the operation not to be performed in-place, then use a = a + b. If you're ambivalent on the in-place issue and just want to write polymorphic code, that's when you should consider using +=.