[Python-ideas] with-statement syntactic quirk

Yuval Greenfield ubershmekel at gmail.com
Thu Nov 1 23:06:20 CET 2012


On Thu, Nov 1, 2012 at 1:40 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:

> I've been remiss in not mentioning the new alternative in 3.3 for
> handling nesting of complex context management stacks:
>
>     with contextlib.ExitStack() as cm:
>         p1 = cm.enter_context(open('/etc/passwd'))
>         p2 = cm.enter_context(open('/etc/passwd'))
>
> (Note: ExitStack is really intended for cases where the number of
> context managers involved varies dynamically, such as when you want to
> make a CM optional, but you *can* use it for static cases if it seems
> appropriate)
>
>
Go's "defer" is quite a neat solution for these hassles if anyone's in the
mood for a time machine discussion.

http://golang.org/doc/effective_go.html#defer

Yuval Greenfield
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20121102/4dd7f154/attachment.html>


More information about the Python-ideas mailing list