Python vs Java garbage collection?

Ype Kingma ykingma at accessforall.nl
Mon Dec 23 08:04:01 EST 2002


Andrew Dalke wrote:

> Stuart D. Gathman wrote:
>> The problem with Python reference counting, is that it encourages sloppy
>> programming like:
>> 
>>   data = open('myfile','r').read()

<snip>

> 
> So if you want to be really, really correct, you might try
> something more like
> 
> fp = open('myfile', 'r')
> try:
>    data = fp.read()
> finally:
>    try:
>      fp.close()
>    except IOError:
>      pass

To be correct the exception traceback should be printed
because that is what happens to uncaught exceptions during
__del__().

> See why automatic garbage collection is so nice?

Jython has automatic garbage collection, but it does need
try/finally constructs because of the unspecified delay
before the __del__() call.

Regards,
Ype




More information about the Python-list mailing list