Python vs Java garbage collection?

Erik Max Francis max at alcyone.com
Sun Dec 22 18:25:04 EST 2002


John Roth wrote:

> I'm not certain I'd call this sloppy programming. It's a language
> design choice. What you seem to be saying is that the garbage
> collector shouldn't be required to call cleanup methods when it
> releases garbage objects.

The problem is that it _is_ sloppy programming, even within Python.  In
_CPython_, you're pretty much guaranteed that the file object will get
reclaimed (and thus closed) rather quickly.  You've no such guarantee in
other Python implementations, such as Jython (where the __del__ method
calling and reclaiming is done with finalizers) and thus happens
whenever the object gets garbage collected, which may be soon after the
statement executes or long after.

Even in Python, you should make sure that code which acquires resources
releases them in a timely manner.  Don't rely on reference counting and
object reclamation to do this, because you'll be asking for trouble in
other Python implementations.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ God will forgive me; that's his business.
\__/ Heinrich Heine
    Maths reference / http://www.alcyone.com/max/reference/maths/
 A mathematics reference.



More information about the Python-list mailing list