[Python-Dev] PEP 550 v4

Nick Coghlan ncoghlan at gmail.com
Wed Aug 30 02:47:07 EDT 2017


On 30 August 2017 at 16:40, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Writing an "update_parent_context" decorator is also trivial (and will
> work for both sync and async generators):
>
>     def update_parent_context(gf):
>         @functools.wraps(gf):
>         def wrapper(*args, **kwds):
>             gen = gf(*args, **kwds):
>             gen.__logical_context__ = None
>             return gen
>         return wrapper
[snip]
> While I'm not sure how much practical use it will see, I do think it's
> important to preserve the *ability* to transparently refactor
> generators using yield from - I'm just OK with such a refactoring
> becoming "yield from update_parent_context(subgen())" instead of the
> current "yield from subgen()" (as I think *not* updating the parent
> context is a better default than updating it).

Oops, I got mixed up between whether I thought this should be a
decorator or an explicitly called helper function. One option would be
to provide both:

    def update_parent_context(gen):
        ""Configures a generator-iterator to update its caller's
context variables""""
        gen.__logical_context__ = None
        return gen

    def updates_parent_context(gf):
        ""Wraps a generator function's instances with update_parent_context""""
        @functools.wraps(gf):
        def wrapper(*args, **kwds):
            return update_parent_context(gf(*args, **kwds))
        return wrapper

Cheers,
Nick.

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


More information about the Python-Dev mailing list