[Python-ideas] Generalized version of contextlib.close

francismb francismb at email.de
Sun Apr 1 08:42:33 EDT 2018



On 03/26/2018 10:35 AM, Roberto Martínez wrote:
> Hi,
> 
> sometimes I need to use contextlib.close but over methods with a different
> name, for example stop(), halt(), etc. For those cases I have to write my
> own contextlib.close specialized version with a hard-coded method name.
> 
> I think adding a "method" argument to contextlib.close can be very useful:
> 
> @contextmanager
> def closing(thing, method="close"):
>     try:
>         yield thing
>     finally:
>         getattr(thing, method)()
> 
+1



More information about the Python-ideas mailing list