I would like to create a Python class which contains a temporary file.
If I use the usual tempfile.TemporaryFile()
with a context manager to create a member variable in the constructor, then the context manager will close/delete the temporary file when the constructor exits. This is no good because I want the file to exist for the lifetime of the class.
I see that I could create my own context managed class using __enter__
and __exit__
methods, does anyone have any examples of this? (Maybe I just need to add a line to delete the file to the example in the link?)
Or maybe there's a better way of doing this?