<p><br>
On Nov 2, 2012 8:06 AM, "Yuval Greenfield" <<a href="mailto:ubershmekel@gmail.com">ubershmekel@gmail.com</a>> wrote:<br>
><br>
> On Thu, Nov 1, 2012 at 1:40 PM, Nick Coghlan <<a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>> wrote:<br>
>><br>
>> I've been remiss in not mentioning the new alternative in 3.3 for<br>
>> handling nesting of complex context management stacks:<br>
>><br>
>>     with contextlib.ExitStack() as cm:<br>
>>         p1 = cm.enter_context(open('/etc/passwd'))<br>
>>         p2 = cm.enter_context(open('/etc/passwd'))<br>
>><br>
>> (Note: ExitStack is really intended for cases where the number of<br>
>> context managers involved varies dynamically, such as when you want to<br>
>> make a CM optional, but you *can* use it for static cases if it seems<br>
>> appropriate)<br>
>><br>
><br>
> Go's "defer" is quite a neat solution for these hassles if anyone's in the mood for a time machine discussion.<br>
><br>
> <a href="http://golang.org/doc/effective_go.html#defer">http://golang.org/doc/effective_go.html#defer</a></p>
<p>Go was one of the reference points for the ExitStack design (it's a large part of why the API also supports providing callbacks directly to the exit stack, not just as context managers).</p>
<p>Cheers,<br>
Nick.</p>
<p>--<br>
Sent from my phone, thus the relative brevity :) <br>
><br>
> Yuval Greenfield</p>