Nesting Python context managers
The easy way to create context managers is with contextlib.contextmanager
. Something like this:
@contextlib.contextmanager
def write_on_change_file(filename):
with tempfile.TemporaryFile('r+') as temporary_file:
yield temporary_file
try:
... some saving logic that you had in __exit__ ...
Then use with write_on_change_file(...) as f:
.
The body of the with
statement will be executed “instead of” the yield
. Wrap the yield
itself in a try
block if you want to catch any exceptions that happen in the body.
The temporary file will always be properly closed (when its with
block ends).