Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > question on garbage collection for python

Reply
Thread Tools

question on garbage collection for python

 
 
David Stockwell
Guest
Posts: n/a
 
      06-01-2004
Hi,

Does Python leave things in an unknown state?

say I do this in my python code


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

data = someFile.read()

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
file.

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,

David

-------
Tracfone: http://cellphone.duneram.com/index.html
Cam: http://www.duneram.com/cam/index.html
Tax: http://www.duneram.com/index.html

__________________________________________________ _______________
MSN Toolbar provides one-click access to Hotmail from any Web page – FREE
download! http://toolbar.msn.click-url.com/go/...ave/direct/01/


 
Reply With Quote
 
 
 
 
Matteo Dell'Amico
Guest
Posts: n/a
 
      06-01-2004
David Stockwell wrote:
> 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?


It isn't specified: it will be collected before the end of the program.
I think that CPython would do it when the refcount goes to 0, whereas in
Jython it will happen "sometimes in the future". Don't count on it, though!

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


The __del__ method.

--
Ciao,
Matteo
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python leaks in cyclic garbage collection moerchendiser2k3 Python 6 02-21-2011 09:10 AM
Newbie question about python garbage collection when keeping only areference to an object's member George Burdell Python 3 11-12-2010 10:20 PM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
RE: Python's garbage collection was Re: Python reliability Delaney, Timothy (Tim) Python 20 10-14-2005 05:27 PM
Templates - Garbage In Garbage Not Out ramiro_b@yahoo.com C++ 1 07-25-2005 04:48 PM



Advertisments