Python vs Java garbage collection?

Erik Max Francis max at alcyone.com
Mon Dec 23 04:47:45 EST 2002


Ype Kingma wrote:

> Untested code:
> 
> def openread(fname):
>     try:
>         fp = open(fname)
>         return fp.read()
>     finally:
>         fp.close()
> 
> Python has enough syntax already.

Really this should be:

	fp = open(fname)
	try:
	    return fp.read()
	finally:
	    fp.close()

If open throws, then the fp name will not be bound, so on the way out
the finally clause will generate a NameError.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ There is nothing so subject to the inconstancy of fortune as war.
\__/ Miguel de Cervantes
    EmPy / http://www.alcyone.com/pyos/empy/
 A templating system for Python.



More information about the Python-list mailing list