question on garbage collection for python

David Stockwell winexpert at
Tue Jun 1 15:16:00 CEST 2004


Does Python leave things in an unknown state?

say I do this in my python code

someFile = open('somefile' , 'r')

data =

someFile = 3

At this point I would hope python would know to destruct the prior reference 
to someFile by unallocating the memory (under the hood) and closing the 

The questions here are:
If in my code if I forget to close a file, when will the file be closed?  Is 
it when something goes out of scope?  Or will it close when the python 
session ends?

If I define a class of somesort, is there a way I can have a destructor 
method (like I would under C++ ?)

Thanks in advance,



MSN Toolbar provides one-click access to Hotmail from any Web page – FREE 

More information about the Python-list mailing list