Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38812

Re: Awsome Python - chained exceptions

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
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; 'exception': 0.03; 'operator': 0.03; 'syntax': 0.03; 'subject:Python': 0.05; '(so': 0.07; 'granted,': 0.07; 'inserts': 0.07; 'interpreted': 0.07; 'operand': 0.07; 'strings.': 0.07; 'try:': 0.07; 'ugly': 0.07; 'python': 0.09; 'argument,': 0.09; 'does,': 0.09; 'forcing': 0.09; 'handler,': 0.09; 'notation': 0.09; 'precedence': 0.09; 'specifiers': 0.09; 'typeerror:': 0.09; 'def': 0.10; 'assume': 0.11; 'index': 0.13; '"%"': 0.16; "'int'": 0.16; "(it's": 0.16; '*any*': 0.16; '>on': 0.16; 'argument.': 0.16; 'correctly,': 0.16; 'devs': 0.16; 'emit': 0.16; 'for,': 0.16; 'formatting.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'indexerror:': 0.16; 'line)': 0.16; 'parentheses': 0.16; 'unsupported': 0.16; 'which,': 0.16; '{0}': 0.16; 'wed,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'string,': 0.17; '(in': 0.18; 'systems.': 0.18; 'feb': 0.19; '(not': 0.20; 'code.': 0.20; 'written': 0.20; 'fairly': 0.21; '"",': 0.22; '(by': 0.22; 'occurs': 0.22; 'example': 0.23; 'statement': 0.23; 'task': 0.23; 'second': 0.24; 'feature': 0.24; 'least': 0.25; 'header:In-Reply- To:1': 0.25; '(most': 0.27; 'first,': 0.27; 'core': 0.27; '2.6': 0.27; 'is?': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'all.': 0.28; 'block,': 0.29; "d'aprano": 0.29; 'steven': 0.29; 'no,': 0.29; 'probably': 0.29; 'worked': 0.30; 'function': 0.30; 'file': 0.32; 'johnson': 0.32; 'not.': 0.32; "who's": 0.32; 'could': 0.32; 'print': 0.32; 'anywhere': 0.33; 'traceback': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'recommended': 0.33; 'that,': 0.34; 'received:google.com': 0.34; 'clear': 0.35; 'pm,': 0.35; 'similar': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'except': 0.36; 'but': 0.36; '12,': 0.36; 'method': 0.36; 'should': 0.36; 'uses': 0.37; 'quite': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'easily': 0.39; 'think': 0.40; 'your': 0.60; 'easy': 0.60; 'skip:u 10': 0.60; "you've": 0.61; 'improved': 0.62; 'is.': 0.62; 'ever': 0.63; 'details': 0.63; 'more': 0.63; 'making': 0.64; 'percent': 0.65; 'unnecessary': 0.65; 'finally': 0.66; 'suited': 0.71; '2013': 0.84; 'etc,': 0.84; 'pike': 0.84; 'type(s)': 0.84; '1:47': 0.91; 'rick': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=gUB/4KZYRFUCcY3viFP7mkCVJUn/Nyi1zsNlGUgkOgs=; b=Zmj7C7ffjXc6uFBBHwZ72KRYHDkAnrrZv3TWh1ANhK0lq1ch8g331UVDqbhtJKH6do Wr5EkgsPfYc5iaN8HL9gzTKX8P07Mi7bErlK3ARFyxL5/CIzYeCc30NYmzgvtQGru1j8 +ImAS7E8cqVMtGMi3z7+fjfNUqeck5O6OsoF1KO5EUJIbWL7nosFukMEzD8RK2061pRx sG0aCYlnPXGqhDplFGtnrLFKg/+r5H8cChUKukAu6j9KnzXdujSFXoJzLKYaxt7htXQs WEq3Et/CweQxQeQV08n/OOTn6O1E/LSPuLVmJbvpdJBy/UGDiyBdLwWL/KoA9mP+KKvi EEbQ==
MIME-Version 1.0
X-Received by 10.58.253.161 with SMTP id ab1mr27161951ved.55.1360738726591; Tue, 12 Feb 2013 22:58:46 -0800 (PST)
In-Reply-To <1bc6cf63-9219-4798-ae01-dfe9643d96b4@googlegroups.com>
References <5119de00$0$11096$c3e8da3@news.astraweb.com> <1bc6cf63-9219-4798-ae01-dfe9643d96b4@googlegroups.com>
Date Wed, 13 Feb 2013 17:58:46 +1100
Subject Re: Awsome Python - chained exceptions
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1740.1360738735.2939.python-list@python.org> (permalink)
Lines 62
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360738735 news.xs4all.nl 6868 [2001:888:2000:d::a6]:42284
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38812

Show key headers only | View raw


On Wed, Feb 13, 2013 at 1:47 PM, Rick Johnson
<rantingrickjohnson@gmail.com> wrote:
>On Tuesday, February 12, 2013 12:15:29 AM UTC-6, Steven D'Aprano wrote:
>> If you've ever written an exception handler, you've probably written a
>> *buggy* exception handler:
>>
>> def getitem(items, index):
>>     # One-based indexing.
>>     try:
>>         return items[index-1]
>>     except IndexError:
>>         print ("Item at index %d is missing" % index - 1)  # Oops!
>>
>>
>> Unfortunately, when an exception occurs inside an except or finally
>> block, the second exception masks the first, and the reason for the
>> original exception is lost:
>>
>> py> getitem(['one', 'two', 'three'], 5)  # Python 2.6
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>>   File "<stdin>", line 6, in getitem
>> TypeError: unsupported operand type(s) for -: 'str' and 'int'
>
> Which (by showing the offensive line) is quite clear to me.

No, the offending (not offensive) line is "return items[index-1]",
which doesn't feature in your traceback at all. It DOES, however,
feature in the Py3.1 double traceback (it's listed as line 4)..

> 1. You are using the print function (so we can assume you are using Python 3.x)

He is? Could just as easily be the print statement with a single
argument, with unnecessary parentheses around that argument. Which, if
I recall correctly, is one of the recommended approaches for making
2/3 bi-compatible code.

> but then you go and use that old ugly "%" string interpolation syntax crap! when you should have used the format method of strings.
>
>     print("Item at index {0} is missing".format(index-1)) # Oops!
>
> ...Oh Steven, if you only knew how we interpreted the "Oops!", more like "Doh!".

No. Definitely not. Percent interpolation isn't going anywhere - core
devs have said so - and there are many occasions when it is at least
as well suited to the task as .format() is. Also, it's a notation
that's well understood *across languages* and in a variety of message
interpolation systems. Anyone who's worked with *any* of them will
understand that %s inserts a string, %d a number (in decimal), etc,
etc. Granted, the exact details after that may change (eg Python has
%r to emit the representation, while Pike uses %O for "any object",
with similar notation), but the format specifiers and modifiers that
came from C are fairly stable, readable, and compact.

In what way is a trivial example like this improved by the use of
format()? The ONLY thing I can think of is that, by forcing you to put
parentheses around the argument, it avoids the issue from the original
post, which is one of operator precedence - but that's something
that's fairly easy to spot when you know what you're looking for, and
is definitely not specific to string formatting.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Awsome Python - chained exceptions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-12 06:15 +0000
  Re: Awsome Python - chained exceptions Terry Reedy <tjreedy@udel.edu> - 2013-02-12 10:13 -0500
  Re: Awsome Python - chained exceptions Zero Piraeus <schesis@gmail.com> - 2013-02-12 14:01 -0400
    Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 19:01 -0800
      Re: Awsome Python - chained exceptions Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-14 00:00 -0700
        Re: Awsome Python - chained exceptions alex23 <wuwei23@gmail.com> - 2013-02-14 16:10 -0800
    Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 19:01 -0800
  Re: Awsome Python - chained exceptions Ethan Furman <ethan@stoneleaf.us> - 2013-02-12 10:15 -0800
  Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 18:47 -0800
    Re: Awsome Python - chained exceptions Michael Torrie <torriem@gmail.com> - 2013-02-12 21:18 -0700
    Re: Awsome Python - chained exceptions Chris Angelico <rosuav@gmail.com> - 2013-02-13 17:58 +1100
      Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-13 08:14 -0800
        Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-13 08:19 -0800
        Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-13 08:19 -0800
        Re: Awsome Python - chained exceptions Chris Angelico <rosuav@gmail.com> - 2013-02-14 09:10 +1100
          Re: Awsome Python - chained exceptions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-14 06:39 +0000
            Re: Awsome Python - chained exceptions Serhiy Storchaka <storchaka@gmail.com> - 2013-02-15 20:51 +0200
        Re: Awsome Python - chained exceptions Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-02-14 13:01 +0100
          Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-14 18:56 -0800
            Re: Awsome Python - chained exceptions Chris Angelico <rosuav@gmail.com> - 2013-02-15 17:18 +1100
              Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-14 23:51 -0800
                Re: Awsome Python - chained exceptions Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-02-15 10:00 +0100
              Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-14 23:51 -0800
                Re: Awsome Python - chained exceptions alex23 <wuwei23@gmail.com> - 2013-02-17 17:35 -0800
                Re: Awsome Python - chained exceptions Dave Angel <davea@davea.name> - 2013-02-17 20:48 -0500
                Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-17 21:51 -0800
                news.gmane.org (was Re: Awsome Python - chained exceptions Terry Reedy <tjreedy@udel.edu> - 2013-02-18 01:10 -0500
                Re: news.gmane.org (was Re: Awsome Python - chained exceptions rurpy@yahoo.com - 2013-02-18 09:12 -0800
                Re: news.gmane.org (was Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-18 18:32 +0000
                Re: news.gmane.org (was Re: Awsome Python - chained exceptions Terry Reedy <tjreedy@udel.edu> - 2013-02-18 17:45 -0500
                Re: news.gmane.org (was Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-18 06:30 +0000
                Re: news.gmane.org (was Re: Awsome Python - chained exceptions Kwpolska <kwpolska@gmail.com> - 2013-02-18 16:27 +0100
                Re: Awsome Python - chained exceptions alex23 <wuwei23@gmail.com> - 2013-02-18 18:18 -0800
                Re: Awsome Python - chained exceptions rurpy@yahoo.com - 2013-02-19 07:52 -0800
                Re: Awsome Python - chained exceptions rusi <rustompmody@gmail.com> - 2013-02-19 09:14 -0800
                Re: Awsome Python - chained exceptions alex23 <wuwei23@gmail.com> - 2013-02-19 22:57 -0800
                Re: Awsome Python - chained exceptions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-20 22:50 +1100
                Re: Awsome Python - chained exceptions Rotwang <sg552@hotmail.co.uk> - 2013-02-20 16:00 +0000
                RE: Awsome Python - chained exceptions "J. Marc Edwards" <marc.edwards@nimbisservices.com> - 2013-02-20 11:08 -0500
                Re: Awsome Python - chained exceptions rurpy@yahoo.com - 2013-02-20 08:13 -0800
                Re: Awsome Python - chained exceptions Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-02-20 18:49 -0500
                Re: Awsome Python - chained exceptions Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-02-20 18:47 -0500
      Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-13 08:14 -0800
    Re: Awsome Python - chained exceptions Michael Torrie <torriem@gmail.com> - 2013-02-13 00:22 -0700

csiph-web