Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'argument': 0.05; 'assignment': 0.07; 'transform': 0.07; 'augmented': 0.09; 'exception.': 0.09; 'yeah,': 0.09; 'cc:addr:python-list': 0.11; 'assignment.': 0.16; 'ast': 0.16; 'backtrace,': 0.16; 'costs.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'limiting': 0.16; 'tuple': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'error': 0.23; 'effort.': 0.24; "shouldn't": 0.24; 'mon,': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'mention': 0.26; 'skip:" 30': 0.26; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'am,': 0.29; 'raise': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'work.': 0.31; 'code': 0.31; 'catching': 0.31; 'closer': 0.31; 'though.': 0.31; 'tuples': 0.31; 'probably': 0.32; 'skip:c 30': 0.32; 'totally': 0.33; "i'd": 0.34; 'could': 0.34; 'message.': 0.35; 'display': 0.35; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; 'described': 0.36; 'done': 0.36; 'possible': 0.36; 'being': 0.38; 'implement': 0.38; 'issue': 0.38; 'does': 0.39; 'sure': 0.39; 'how': 0.40; 'easy': 0.60; 'around.': 0.60; 'catch': 0.60; 'tell': 0.60; 'hope': 0.61; 'lower': 0.61; 'tag': 0.61; 'worth': 0.66; 'mar': 0.68; 'home.': 0.72; 'costly': 0.84; 'valid,': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=ccUuTiUFfHU/p1AQ+HdHXulEOA67B07WFoucLtUmG0I=; b=WA9a8Ylr7f0S75Ty9M+K7Z8k1PuPfxAUdagpNWBiWbibfdmrTVA67KJ06Uah9pzLaX SAuPk4ZZjosU5XFKSak0panp0p1lTb7sBTJuiC2Go27fIuYwXhNsJCjwMxbw3oYo2LJs bLHWqsjG7DcYWKnh1Z99k/qZYeQyLn9PlyTA4fjZW+6iA6MZm/c9SlHxXqHMcjpT4fMa UFEKa5DtAWNIIUG7QzB1jDbmchhvcM1U2VaV+JaIe/VASMU57KFzsT0SXJm9YygxFSxb q9uw1DJjgBsXEnHYssE0w2RUW3c/KtkYp6YnmoL1Yg5PLCe/1ilcRNeFtcTFEYDNhtDD kMdA== MIME-Version: 1.0 X-Received: by 10.66.181.70 with SMTP id du6mr35254681pac.23.1394395597324; Sun, 09 Mar 2014 13:06:37 -0700 (PDT) In-Reply-To: References: Date: Mon, 10 Mar 2014 07:06:37 +1100 Subject: Re: Tuples and immutability From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394395600 news.xs4all.nl 2947 [2001:888:2000:d::a6]:38042 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68090 On Mon, Mar 10, 2014 at 6:57 AM, Joshua Landau wrote: > I would probably implement it closer to home. Inside > tuple.__getitem__, there would be something like > > if context_is_augmented_assignment(): > raise TypeError(message+warning) > else: > raise TypeError(message) > > which would have much lower technical costs. It does depend on how > costly "context_is_augmented_assignment" is, though. A speed argument > is totally valid, but I'd hope it's possible to work around. Yeah, I'm not sure there's an easy way to tell the tuple that it's an augmented assignment. You might be able to look at the backtrace, but that's tricky. In theory, you could catch TypeError after any augmented assignment, and check several things: 1) The target object does not have __setitem__ 2) The object being manipulated does have __iadd__ (or corresponding for others) 3) The error is that item assignment is not possible and if all three are the case, then add a tag to the message. But this is best done by catching the exception. Otherwise you'd be limiting this to tuples and lists; not to mention that this is really only an issue of error reporting, and correct code shouldn't be tripping this at all. So put a check like that at the place where you display the error, if you can. The AST transform that I described would also work. But I really think it's not worth the effort. If you get this error, understand that it may have consequences. ChrisA