On Sun, 27 Feb 2011 00:39:16 +1000
Nick Coghlan
On Sat, Feb 26, 2011 at 10:52 PM, cool-RR
wrote: Hello, I noticed that the `TemporaryDirectory` context manager creates the folder on `__init__` rather than on `__enter__`, resulting in complexity, bugs, and hackarounds in `__del__`. I assume there's a good reason for this decision. What is it?
From the docstring: "This has the same behavior as mkdtemp but can be used as a context manager." Like files, it *can* be used as a context manager, but doesn't have to be.
Also, the complexity wouldn't go away even if the directory creation was delayed until the __enter__ invocation. People can still call __enter__ directly, so __del__ would still be obliged to try to clear things up as best it could.
Calling __enter__ directly without caring to call __exit__ afterwards should certainly be considered a user bug (not to mention of dubious utility in this case, since it's easier to spell mkdtemp() than TemporaryDirectory().__enter__()). Regards Antoine.