Python vs Java garbage collection?

Erik Max Francis max at alcyone.com
Mon Dec 23 02:03:54 EST 2002


Courageous wrote:

> I wish I could provide an objective metric for this, but I think it's
> mostly opinion and subjective. One bit of objective truth is this,
> however: file.close() says quite clearly "I'm done with this." While
> one can infer that if the reference to file no longer exists, one is
> done with the file, that's an additional inferential step. Explicit
> is better than implicit, sometimes. I think this is one of those
> times.

I'm a firm believer in "Explicit is better than implicit" in general
terms, but this is one of those cases where it's verging on a
requirement.  Python as a language itself doesn't make any guarantees on
whether finalizers will get called in a timely manner, so if you have
some external resource tied to a Python object, letting the last
reference to it go away does not give any guarantees on when the
resource really will be released.

In the present implementation of CPython, of course, it happens
immediately after the last reference goes away, but Python as a language
does not make that guarantee, and at least one implementation, Jython,
follows Java's garbage collection model and makes no such guarantee. 
Indeed, you've really no guarantee that CPython itself will even
continue to behave that way!

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Whoever named it necking was a poor judge of anatomy.
\__/ Groucho Marx
    CAGE / http://www.alcyone.com/pyos/cage/
 A cellular automaton simulation system in Python.



More information about the Python-list mailing list