[Python-ideas] Anonymous blocks (again):

Nick Coghlan ncoghlan at gmail.com
Tue May 14 08:36:27 CEST 2013


On Tue, May 14, 2013 at 7:35 AM, Juancarlo Añez <apalala at gmail.com> wrote:
> Just a reminder of why I started this discussion.
>
> I couldn't find a way to have:
>
> with context():
>     do_this()
>     do_that()
>
>
> in which the embedded block would be executed multiple times, through yield.
> My fault! Lack of understanding about the context manager protocol.
>
> It is not possible to yield to the block multiple times because an exception
> within the block will exit the context no matter what __exit__() does.

Have you considered an iterator that produces context managers rather
than the other way around?

Cheers,
Nick.

--
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list