[Python-Dev] cpython: Rename contextlib.ignored() to contextlib.ignore(). (original) (raw)
Tim Delaney timothy.c.delaney at gmail.com
Tue Oct 15 21:59:55 CEST 2013
- Previous message: [Python-Dev] cpython: Rename contextlib.ignored() to contextlib.ignore().
- Next message: [Python-Dev] cpython: Rename contextlib.ignored() to contextlib.ignore().
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 16 October 2013 05:17, Alexander Belopolsky < alexander.belopolsky at gmail.com> wrote:
On Tue, Oct 15, 2013 at 12:45 PM, Ethan Furman <ethan at stoneleaf.us> wrote: > with trap(OSError) as cm: > os.unlink('missing.txt') > if cm.exc: > dosomething()
.. and why is this better than try: os.unlink('missing.txt') except OSError as exc: dosomething()
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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20131016/71b95214/attachment.html>
- Previous message: [Python-Dev] cpython: Rename contextlib.ignored() to contextlib.ignore().
- Next message: [Python-Dev] cpython: Rename contextlib.ignored() to contextlib.ignore().
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]