Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'assignment': 0.07; 'odd': 0.07; 'augmented': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'sections': 0.14; "wouldn't": 0.14; 'ast': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'get,': 0.16; 'locating': 0.16; 'mutated': 0.16; 'occurred.': 0.16; 'repl': 0.16; 'reraise': 0.16; 'top-level': 0.16; 'typeerror:': 0.16; 'exception': 0.16; 'sat,': 0.16; 'ignore': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'variable': 0.18; 'basically': 0.19; '>>>': 0.22; 'import': 0.22; 'saying': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'certainly': 0.24; 'exists': 0.24; 'text.': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'first,': 0.26; 'shown': 0.26; 'gets': 0.27; 'header:In-Reply- To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'raise': 0.29; '(like': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'that.': 0.31; '"",': 0.31; 'raised': 0.31; 'file': 0.32; 'probably': 0.32; '(most': 0.33; 'not.': 0.33; 'could': 0.34; 'except': 0.35; 'possible.': 0.35; 'done.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'doing': 0.36; 'possible': 0.36; 'should': 0.36; 'two': 0.37; 'server': 0.38; 'whatever': 0.38; 'recent': 0.39; 'does': 0.39; 'sure': 0.39; 'how': 0.40; 'even': 0.60; 'remove': 0.60; 'catch': 0.60; 'results.': 0.60; 'skip:t 30': 0.61; 'matter': 0.61; "you're": 0.61; 'such': 0.63; 'happen': 0.63; 'more': 0.64; 'benefit': 0.68; 'mar': 0.68; 'done;': 0.84; 'isolate': 0.84; 'wish,': 0.91; '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=p/ZXh8njplBO3JgpPR5vhsw6VlN+ip2Ai02uBs5yopk=; b=vFDERCxWdRt4uLLCksBXCsfKuecFfA3Cqhyb8AsMFEoqd0CrB5QmnPjkdzTkA3GL75 TcCOvAC4SVXdkt/X365TKZ+itFr49yTImsDR/kplKXcqvLK/8QNqAgmY8jhX+kIrCZGr eLlQ104HXPSBAeUsEUBhxTe0Xeknjju3AteJjiBh1McH/C+WHQedCu3/XRcWFOBN10e+ sw09kkmvJZYRXGTViPu6yJMfnjTmhccGnDH2LcERi4mVTg9GwCLtM5xwlopS6ynNAtoE DnoWT0qQ+Mmnbfa99ezTKHnzLTkADaYX4C+UjRXKO4NYGYyOreiCYWO80nM2XJFllnuO rrrg== MIME-Version: 1.0 X-Received: by 10.66.27.202 with SMTP id v10mr35097397pag.127.1394388804697; Sun, 09 Mar 2014 11:13:24 -0700 (PDT) In-Reply-To: References: Date: Mon, 10 Mar 2014 05:13:24 +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: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394388814 news.xs4all.nl 2952 [2001:888:2000:d::a6]:39995 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68085 On Mon, Mar 10, 2014 at 4:54 AM, Joshua Landau wrote: > On 28 February 2014 14:43, Chris Angelico wrote: >> On Sat, Mar 1, 2014 at 1:41 AM, Joshua Landau wrote: >>> Would it be better to add a check here, such that if this gets raised >>> to the top-level it includes a warning ("Addition was inplace; >>> variable probably mutated despite assignment failure")? >> >> That'd require figuring out whether or not the variable was actually >> mutated, and that's pretty hard to work out. > > It does not. First, the "warning" is actually an attachment to the > exception so is only shown if the exception is uncaught. This should > basically never happen in working code. The warning exists only to > remove likely misunderstanding in these odd cases. > > Even if "x = (1,); x[0] += 1" warned "addition was inplace; possible > mutation occurred" or whatever phrasing you wish, this would only > cause a quick check of the results. I think I see what you're saying here. But ignore "top-level"; this should just be a part of the exception message, no matter what. Otherwise things that recreate the REPL (like IDLE) or that isolate two sections of the code (like a web server framework) wouldn't get the benefit, because the exception's not caught at the true top-level. >>> x=1, >>> x[0]+=0 Traceback (most recent call last): File "", line 1, in x[0]+=0 TypeError: 'tuple' object does not support item assignment What you're saying is that this should notice that it's doing an augmented assignment and give some more text. This can be done; all you need to do is catch the error and reraise it with more info: >>> try: x[0]+=0 except TypeError as e: if 'does not support item assignment' in e.args[0]: raise TypeError(e.args[0]+"\nAugmented assignment used; mutation may have occurred.") from None raise Traceback (most recent call last): File "", line 5, in raise TypeError(e.args[0]+"\nAugmented assignment used; mutation may have occurred.") from None TypeError: 'tuple' object does not support item assignment Augmented assignment used; mutation may have occurred. Now you can look at writing an import hook that does an AST transform, locating every instance of item assignment and wrapping it like that. It's certainly possible. I'm not sure how much benefit you'd get, but it could be done. ChrisA