osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Python3 - How do I import a class from another file


Hi Chris,

> The most important distinction, which that note is emphasizing, is
> that the "del" statement removes just one reference, and if there are
> other references, then __del__ will not be called.

No argument there, that's how reference counting works, and it's clear 
from the docs. What is not clear from the documentation is not if or why 
or how but *when* the __del__ method is eventually called. The doc says: 
"when [an object's] reference count reaches zero." Initially I read that 
to mean "immediately upon the reference count hitting zero," but a 
couple paragraphs down we find this: "In particular:  __del__() can be 
invoked when arbitrary code is being executed, including from any 
arbitrary thread. [...]"

I used to believe that the __del__ method was called immediately after 
refcount = zero, and that only the memory management system's functions 
(typically called from __del__) could take their time doing their 
things. Not true, it seems: Calling __del__() can be deferred, and 
PyMem_Free() et. al. migh not immediately "do"  anything as well.

I've come to see the __del__ method as a tool to get rid of an object 
(and its resources) which can be used by the Python interpreter at its 
own discretion when and if it feels the need to do so, which may be 
anything between immediately after ob_refcnt == 0 and not at all. That 
said, I can't think of any reason to explicitly define __del__() in a 
pure Python class. Do you have an example?