Python vs Java garbage collection?

Ype Kingma ykingma at accessforall.nl
Mon Dec 23 04:14:36 EST 2002


Stuart,

> On Sat, 21 Dec 2002 16:19:19 -0500, Robert Oschler wrote:

<snip>
 
> 
> The problem with Python reference counting, is that it encourages sloppy
> programming like:
> 
>   data = open('myfile','r').read()
> 
> depending on the reference counting GC to release and close the file
> object immediately when read() returns.  This habit must be broken before
> Python can evolve to Lisp like speed.  The proper code:
> 
>   fp = open('myfile','r')
>   data = fp.read()
>   fp.close()
> 
> is not as pretty.  Perhaps some clever pythonista will invent some
> syntactic sugar to help the medicine go down.

Untested code:

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

Python has enough syntax already.

Have fun,
Ype


-- 
email at xs4all.nl



More information about the Python-list mailing list