[Python-ideas] PEP draft: context variables

Nick Coghlan ncoghlan at gmail.com
Mon Oct 16 08:05:51 EDT 2017

On 16 October 2017 at 21:08, Juancarlo Añez <apalala at gmail.com> wrote:

> Interestingly, thinking about the problem in terms of exception handling
>> flow reminded me of the fact that having a generator-iterator yield while
>> inside a with statement or try/except block is already considered an
>> anti-pattern in many situations, precisely because it means that any
>> exceptions that get thrown in (including GeneratorExit) will be intercepted
>> when that may not be what the author really intended.
> It all works fine now:
> https://github.com/neogeny/TatSu/blob/master/tatsu/contexts.py
> So, I have a strong requirement: whatever is decided on this PEP...
> Please don't break it? (or make it illegal)

The "anti-pattern in many situations" qualifier was there because there are
cases where it's explicitly expected to work, and isn't an anti-pattern at
all (e.g. when the generator is decorated with contextlib.contextmanager,
or when you're using a context manager to hold open an external resource
like a file until the generator is closed).

So this wasn't intended as an argument for changing anything - rather, it's
about my changing my perspective on how beneficial it would be to have
generators default to maintaining their own distinct logical context (which
then becomes an argument for *not* changing anything).


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171016/73f10ba5/attachment.html>

More information about the Python-ideas mailing list