Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your <code>__del__</code> method assumes that the class is still present by the time it is called.</p> <p>This assumption is incorrect. <code>Groupclass</code> has already been cleared when your Python program exits and is now set to <code>None</code>.</p> <p>Test if the global reference to the class still exists first:</p> <pre><code>def __del__(self): if Groupclass: Groupclass.count -= 1 </code></pre> <p>or use <code>type()</code> to get the local reference:</p> <pre><code>def __del__(self): type(self).count -= 1 </code></pre> <p>but do note that this means that the semantics for <code>count</code> change if <code>Groupclass</code> is subclassed (each subclass gets a <code>.count</code> attribute versus only <code>Groupclass</code> having a <code>.count</code> attribute).</p> <p>Quoting from the <code>__del__</code> hook documentation:</p> <blockquote> <p><strong>Warning</strong>: Due to the precarious circumstances under which <code>__del__()</code> methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to <code>sys.stderr</code> instead. Also, when <code>__del__()</code> is invoked in response to a module being deleted (e.g., when execution of the program is done), other globals referenced by the <code>__del__()</code> method may already have been deleted or in the process of being torn down (e.g. the import machinery shutting down). For this reason, <code>__del__()</code> methods should do the absolute minimum needed to maintain external invariants. Starting with version 1.5, Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted; if no other references to such globals exist, this may help in assuring that imported modules are still available at the time when the <code>__del__()</code> method is called.</p> </blockquote> <p>If you are using Python 3, two additional notes apply:</p> <ul> <li><p>CPython 3.3 automatically applies a <a href="https://docs.python.org/3/reference/datamodel.html#object.__hash__" rel="noreferrer">randomized hash salt</a> to the <code>str</code> keys used in a <code>globals</code> dictionary; this also affects the order in which globals are cleaned up, and it <em>could</em> be that you see the problem on only <em>some</em> of the runs.</p></li> <li><p>CPython 3.4 no longer sets globals to <code>None</code> (in most cases), as per <a href="https://docs.python.org/3/whatsnew/3.4.html#whatsnew-pep-442" rel="noreferrer">Safe Object Finalization</a>; see <a href="http://www.python.org/dev/peps/pep-0442/" rel="noreferrer">PEP 442</a>.</p></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload