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

Python3 - How do I import a class from another file

Am 10.12.2019 22:33 schrieb Paul Moore:

> You do understand that the reference counting garbage collector is an
> implementation detail of the CPython implementation *only*, don't you?

I don't think that's true. Here's a sentonce from near the top of the 
"gc" module documentation of Python 3:

"Since the collector supplements the reference counting already used in 
Python, you can disable the collector if you are sure your program does 
not create reference cycles."

The way I read this is that Python automatically and immediately deletes 
objects once their refcount goes to zero, and the garbage collector only 
kicks in case of circular references or other obscure circumstances. The 
documentation makes no reference to the specific Python implementation, 
so I believe this is true for CPython as well as others.

To be specific: Within the semantics of the Python documentation, 
freeing the resources used by an object by explicitly or implicitly 
using "del" is not garbage collection. Python garbage collection is like 
street cleaning in real life: If everybody looked after their own trash, 
we wouldn't need a municipal service to do it.

When I first read about the Python garbage collector I was puzzled at 
the possibility of disabling it, thinking that over time a long-running 
program would fill all memory because no object's resources would ever 
be freed. But that is clearly  not the case. Even Instagram can live 
without garbage collecton (although if you look how much garbage is on 
Instagram, maybe they should re-enable it):


> The (implementation independent) language semantics makes no assertion
> about what form of garbage collection is used, and under other garbage
> collectors, there can be an indefinite delay between the last
> reference to a value being lost and the object being collected (which
> is when __del__ gets called).

Only when there are circular references. Otherwise every Python 
implementation will delete objects once their refcount goes to zero, 
wven when there is no garbage collection at all, see the doc.

> There is not even a guarantee that CPython will retain the reference 
> counting GC in future versions.

There is no "reference counting GC" in Python. Freeing objects based on 
their reference count going to zero happens independently of the GC, see 
the official docs quoted above.