473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

question on garbage collection for python

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/
Jul 18 '05 #1
1 1112
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
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peter Kwan | last post by:
Hi, I believe I have discovered a bug in Python 2.3. Could anyone suggest a get around? When I tested my existing Python code with the newly released Python 2.3, I get the following warning: ...
2
by: Chaman Singh Verma | last post by:
Hello, I am trying to integrate C++ with Python. I read that Python does automatic garbage collection. I am creating new objects in C++ and passing to Python, I don't know now who should control...
2
by: James S | last post by:
Hi, Basically I've been fighting with this code for a few days now and can't seem to work around this problem. Included is the output, the program I use to get this error and the source code for...
34
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple,...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
0
by: David Hirschfield | last post by:
Question from a post to pygtk list...but it probably would be better answered here: I encountered a nasty problem with an external module conflicting with my python threads recently, and right...
16
by: Stef Mientki | last post by:
If I create a large array of data or class, how do I destroy it (when not needed anymore) ? Assign it to an empty list ? thanks, Stef Mientki
22
by: Tom Wright | last post by:
Hi all I suspect I may be missing something vital here, but Python's garbage collection doesn't seem to work as I expect it to. Here's a small test program which shows the problem on python 2.4...
17
by: andreas.eisele | last post by:
I should have been more specific about possible fixes. In a related thread on http://bugs.python.org/issue2607 Amaury Forgeot d'Arc suggested a setting of the GC thresholds that actually...
0
by: Kyle Lanclos | last post by:
I've done a lot of web searching, a fair bit of C-source reading, and quite a lot of miscellaneous head scratching, and I find that I am not necessarily closer to an ideal solution. I have a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.