osdir.com


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

Python3 - How do I import a class from another file


On 11/12/19 7:47 am, R.Wieser wrote:
> what happens when the reference becomes zero: is the __del__
> method called directly (as I find logical), or is it only called when the
> garbage collector actually removes the instance from memory (which Chris
> thinks what happens) ?

In CPython, these are the same thing. As soon as the reference
count becomes zero, the __del__ method is called *and* the object
is removed from memory.

This remains true even in the presence of reference cycles. As
long as the object is part of a cycle, it's reference count
is not zero. When the cyclic garbage collector gets around to
noticing the cycle, it breaks it, causing the reference counts
of the objects in it to become zero, in turn causing them to
be collected.

In a Python implementation that doesn't use reference counts,
using "del" on the last reference to an object probably isn't
going to do anything to the object.

You seem to think it would be logical for it to call the __del__
method there and then, instead of waiting until the object is
collected. But without reference counting, it has no way of
*knowing* that it's the last reference until the garbage
collector does its thing.

To summarise, your logic on this point is flawed.

> As a simple bit of testcode has shown me (read my reply to
> Chris).

We're not saying that your code won't work -- in current
CPython it will. We're saying that it relies on an
implementation detail. To demonstrate that with a code example,
it would have to be run on a different implementation of Python
that didn't use reference counting, such as Jython.

-- 
Greg