of destructors, open files and garbage collection

Terry Reedy tjreedy at udel.edu
Thu May 24 15:45:59 EDT 2007


"massimo s." <devicerandom at gmail.com> wrote in message 
news:1180021256.619953.80500 at q66g2000hsg.googlegroups.com...
| Hi,
|
| Python 2.4, Kubuntu 6.06. I'm no professional programmer (I am a ph.d.
| student in biophysics) but I have a fair knowledge of Python.
|
| I have a for loop that looks like the following :
|
| for item in long_list:
|       foo(item)
|
| def foo(item):
|       item.create_blah() #<--this creates item.blah; by doing that it
| opens a file and leaves it open until blah.__del__() is called
|
| Now, what I thought is that if I call
|
| del(item)
|
| it will delete item

No, it removes the association between the name 'item' and the object it is 
currently bound to.  In CPython, removing the last such reference will 
cause the object to be gc'ed.  In other implementations, actual deletion 
may occur later.  You probably should close the files directly and arrange 
code so that you can do so before too many are open.

tjr






More information about the Python-list mailing list