[Python-ideas] Generalized version of contextlib.close

Nick Coghlan ncoghlan at gmail.com
Mon Apr 2 03:20:57 EDT 2018


On 26 March 2018 at 18:35, Roberto Martínez <robertomartinezp at gmail.com> wrote:
> @contextmanager
> def calling(fn, *args, **kwargs):
>     try:
>         yield
>     finally:
>         fn(*args, **kwargs)

I'd be more amenable to a proposal along these lines (rather than
adding a parameter to closing), as it more closely resembles the way
that contextlib.ExitStack.callback already works:

    with contextlib.ExitStack() as stack:
        stack.callback(fn, *args, **kwds)
        ...

In cases where you just want to call a single operation and don't need
the extra complexity and overhead of the dynamic stack, then it would
be nice to be able to instead write:

    with contextlib.callback(fn, *args, **kwds):
        ...

Cheers,
Nick.

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


More information about the Python-ideas mailing list