Re: [Python-Dev] cpython: Rename contextlib.ignored() to contextlib.ignore().

2013年10月15日 13:01:37 -0700

On 16 October 2013 05:17, Alexander Belopolsky <
[email protected]> wrote:
> On Tue, Oct 15, 2013 at 12:45 PM, Ethan Furman <[email protected]> wrote:
> > with trap(OSError) as cm:
> > os.unlink('missing.txt')
> > if cm.exc:
> > do_something()
>
> .. and why is this better than
>
> try:
> os.unlink('missing.txt')
> except OSError as exc:
> do_something()
It would allow you to perform a series of operations then process the any
exceptions all together e.g.
with trap(OSError) as cm1:
 os.unlink('missing.txt')
with trap(OSError) as cm2:
 os.unlink('other_missing.txt')
with trap(OSError) as cm3:
 os.unlink('another_missing.txt')
for cm in (cm1, cm2, cm3):
 if cm.exc:
 do_something(cm.exc)
An equivalent implementation would be:
exceptions = []
try:
 os.unlink('missing.txt')
except OSError as exc:
 exceptions.append(exc)
try:
 os.unlink('missing.txt')
except OSError as exc:
 exceptions.append(exc)
try:
 os.unlink('missing.txt')
except OSError as exc:
 exceptions.append(exc)
for exc in exceptions:
 if exc:
 do_something(exc)
Tim Delaney
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to