<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 16 October 2013 05:17, Alexander Belopolsky <span dir="ltr"><<a href="mailto:alexander.belopolsky@gmail.com" target="_blank">alexander.belopolsky@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="im">On Tue, Oct 15, 2013 at 12:45 PM, Ethan Furman <<a href="mailto:ethan@stoneleaf.us">ethan@stoneleaf.us</a>> wrote:<br>

>     with trap(OSError) as cm:<br>
>         os.unlink('missing.txt')<br>
>     if cm.exc:<br>
>         do_something()<br>
<br>
</div>.. and why is this better than<br>
<br>
try:<br>
   os.unlink('missing.txt')<br>
except OSError as exc:<br>
   do_something()</blockquote><div><br></div><div>It would allow you to perform a series of operations then process the any exceptions all together e.g.</div><div><br></div><div><div>with trap(OSError) as cm1:</div><div>
    os.unlink('missing.txt')</div><div><br></div><div>with trap(OSError) as cm2:</div><div>    os.unlink('other_missing.txt')</div><div><br></div><div>with trap(OSError) as cm3:</div><div>    os.unlink('another_missing.txt')</div>
<div><br></div><div>for cm in (cm1, cm2, cm3):</div><div>    if cm.exc:</div><div>        do_something(cm.exc)</div></div><div><br></div><div>An equivalent implementation would be:</div><div><br></div><div><div>exceptions = []</div>
<div><br></div><div>try:</div><div>    os.unlink('missing.txt')</div><div>except OSError as exc:</div><div>    exceptions.append(exc)</div><div><br></div><div>try:</div><div>    os.unlink('missing.txt')</div>
<div>except OSError as exc:</div><div>    exceptions.append(exc)</div><div><br></div><div>try:</div><div>    os.unlink('missing.txt')</div><div>except OSError as exc:</div><div>    exceptions.append(exc)</div><div>
<br></div><div>for exc in exceptions:</div><div>    if exc:</div><div>        do_something(exc)</div></div><div><br></div><div>Tim Delaney</div></div></div></div>