Author ncoghlan
Recipients alex, chris.jerdonek, cvrebert, ezio.melotti, loewis, ncoghlan, rhettinger
Date 2012-08-29.09:55:27
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1346234128.86.0.598182482519.issue15806@psf.upfronthosting.co.za>
In-reply-to
Content
(Note: I'm not yet convinced this is a good idea. I'm definitely considering it, though)

As with many context managers, a key benefit here is in the priming effect for readers. In this code:

    try:
       # Whatever
    except (A, B, C):
       pass

the reader doesn't know that (A, B, C) exceptions will be ignored until the end. The with statement form makes it clear before you start reading the code that certain exceptions won't propagate:

    with ignored(A, B, C):
        # Whatever

I'm not worried that it makes things less explicit - it's pretty obvious what a context manager called "ignored" that accepts an arbitrary number of exceptions is going to do.

One other thing it does is interact well with ExitStack - you can stick this in the stack of exit callbacks to suppress exceptions that you don't want to propagate.
History
Date User Action Args
2012-08-29 09:55:28ncoghlansetrecipients: + ncoghlan, loewis, rhettinger, ezio.melotti, alex, cvrebert, chris.jerdonek
2012-08-29 09:55:28ncoghlansetmessageid: <1346234128.86.0.598182482519.issue15806@psf.upfronthosting.co.za>
2012-08-29 09:55:28ncoghlanlinkissue15806 messages
2012-08-29 09:55:27ncoghlancreate