Python vs Java garbage collection?

Peter Hansen peter at engcorp.com
Mon Dec 23 09:29:11 EST 2002


I offer this merely as representative of one of the scenarios that
could happen if one fails to close files explicitly.

Admittedly this is not a typical sequence, but the point is, you cannot
*always* rely on the CPython finalization behaviour even in CPython, so
why waste time guessing when you can, and when you can't?


Python 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> f = open('test.fil', 'w')
>>> f.write('this is a test')
>>>
>>> # other code might be here
...
>>> g = open('test.fil', 'r')
>>> x = g.read()
>>> x
''
>>> print x

>>> hello, x, where are you?
  File "<stdin>", line 1
    hello, x, where are you?
                      ^
SyntaxError: invalid syntax
>>> f.close()
>>> x = g.read()
>>> x
'this is a test'
>>>

-Peter



More information about the Python-list mailing list