with open(path) as f: is a syntax sugar to automatically "close" object f, but it doesn't mean you have to I think use it. A simple f = open(path) and f.close()a helper is still OK.
So why notbetter:
from contextlib import ExitStack, contextmanager class Foo: def __init__(self, in_file_namei, out_file_nameo): self.i = open(in_file_name, 'r')i self.o = openo @contextmanager def multiopen(out_file_namei, 'w'o) : defwith __enter__ExitStack(self): as stack: return self i def= __exit__stack.enter_context(*excopen(i):) self.io = stack.closeenter_context(open(o)) self.o.closeyield Foo(i, o) There are so many similar usages. For example, in requests, __exit__ aThe usage is close to native Sessionopen instance will execute code closing underlying adapters(connection pool). Related code:
class Session(SessionRedirectMixin): # ... omitted defwith __exit__multiopen(selfi_name, *args): self.close() def close(selfo_name): """Closes all adapters and as such the session""" for v in self.adapters.values()foo: v.close()pass