[Python-Dev] PEP 550 v4 (original) (raw)

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 "updateparentcontext" decorator is also trivial (and will work for both sync and async generators):

def updateparentcontext(gf): @functools.wraps(gf): def wrapper(*args, **kwds): gen = gf(*args, **kwds): gen.logicalcontext = 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 updateparentcontext(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