title: Proper finalization in Python
body: <p>I have a bunch of instances, each having a unique tempfile for its use (save data from memory to disk and retrieve them later).</p>
  <p>I want to be sure that at the end of the day, all these files are removed. However, I want to leave a room for a fine-grained control of their deletion. That is, some files may be removed earlier, if needed (e.g. they are too big and not important any more).</p>
  <p>What is the best / recommended way to achieve this?</p>
  <p>May thoughts on that</p>
  <ul>
  <li><p>The <code>try-finalize</code> blocks or <code>with</code> statements are not an option, as we have many files, whose lifetime may overlap each other. Also, it hardly admits the option of finer control.</p>
  </li>
  <li><p>From what I have read, <code>__del__</code> is also not a feasible option, as it is not even guaranteed that it will eventually run (although, it is not entirely clear to me, what are the &quot;risky&quot; cases). Also (if it is still the case), the libraries may not be available when <code>__del__</code> runs.</p>
  </li>
  <li><p><a href="https://docs.python.org/3/library/tempfile.html" rel="nofollow noreferrer"><code>tempfile</code> library</a> seems promising. However, the file is gone after just closing it, which is definitely a bummer, as I want them to be closed (when they perform no operation) to limit the number of open files.</p>
  <ul>
  <li><p>The library promises that the file &quot;will be destroyed as soon as it is closed (including an implicit close when the object is garbage collected).&quot;</p>
  <p>How do they achieve the implicit close? E.g. in C# I would use a (reliable) finalizer, which <code>__del__</code> is not.</p>
  </li>
  </ul>
  </li>
  <li><p><a href="https://docs.python.org/3/library/atexit.html" rel="nofollow noreferrer"><code>atexit</code> library</a> seems to be the best candidate, which can work as a reliable finalizer instead of <code>__del__</code> to implement safe disposable pattern. The only problem, compared to object finalizers, is that it runs truly at-exit, which is rather inconvenient (what if the object eligible to be garbage-collected earlier?).</p>
  <ul>
  <li>Here, the question still stands. How the library achieves that the methods always run? (Except in a really unexpected cases with which is hard to do anything)</li>
  </ul>
  </li>
  </ul>
  <p>In ideal case, it seems that a combination of <code>__del__</code> and <code>atexit</code> library may perform best. That is, the clean-up is both at <code>__del__</code> and the method registered in <code>atexit</code>, while repeated clean-up would be forbidden. If <code>__del__</code> was called, the registered will be removed.</p>
  <p>The only (yet crucial) problem is that <code>__del__</code> won't run if a method is registered at <code>atexit</code>, because a reference to the object exists forever.</p>
  <p>Thus, any suggestion, advice, useful link and so on is welcomed.</p>