Message169357
(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. |
|
Date |
User |
Action |
Args |
2012-08-29 09:55:28 | ncoghlan | set | recipients:
+ ncoghlan, loewis, rhettinger, ezio.melotti, alex, cvrebert, chris.jerdonek |
2012-08-29 09:55:28 | ncoghlan | set | messageid: <1346234128.86.0.598182482519.issue15806@psf.upfronthosting.co.za> |
2012-08-29 09:55:28 | ncoghlan | link | issue15806 messages |
2012-08-29 09:55:27 | ncoghlan | create | |
|