<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman,new york,times,serif;font-size:12pt"><div>There are a number of use-cases for object "cleanup" that are not covered by a generic garbage collector...<br><br>For instance, if an object is "caching" data that needs to be flushed to some persistent resource, then the GC has<br>no idea about this.<br><br>It seems to be that for complex objects, clients of these objects will need to explictly call the objects "cleanup" routine<br>in some type of "finally" clause, that is if the main "thread" of execution is some loop that can terminate either expectedly or<br>unexpectedly....<br><br>Relying on a generic GC is only for simple object cleanup...at least based on what I've read so far.<br><br>Someone mentioned a "context manager" earlier...I may see what this is about as well, since I'm new to the
 language.<br><br>Thanks!<br>Randy<br><br></div><div style="font-family: times new roman,new york,times,serif; font-size: 12pt;"><br><div style="font-family: arial,helvetica,sans-serif; font-size: 13px;"><font size="2" face="Tahoma"><hr size="1"><b><span style="font-weight: bold;">From:</span></b> Albert Hopkins <marduk@letterboxes.org><br><b><span style="font-weight: bold;">To:</span></b> python-list@python.org<br><b><span style="font-weight: bold;">Sent:</span></b> Saturday, March 21, 2009 6:45:20 PM<br><b><span style="font-weight: bold;">Subject:</span></b> Re: __init__ vs. __del__<br></font><br>
On Sat, 2009-03-21 at 17:41 -0700, Randy Turner wrote:<br>> Hi,<br>> <br>> <br>> I was reading a book on Python-3 programming recently and the book<br>> stated that, while there is an __init__ method for initializing<br>> objects, there was a __del__ method but the __del__ method is not<br>> guaranteed to be called when an object is destroyed.<br>> <br>> <br>> If there is code in the __init__ method that allocates resources<br>> (memory, file opens, etc.), how do these resources get cleaned up when<br>> an object is destroyed?  Custom method? <br>> <br><br><br>__del__ is called when the garbage collector is about to destroy an object.<br>There are times when it may not be called like IIRC when the interpreter exits<br>before an object is garbage collected or when there is an exception that causes<br>the object's reference count to stay >0, however...<br><br>In practice you rarely need to implement a __del__
 method.  Memory is handled by the<br>garbage collector.  Most of the time you need not deal with it and in<br>the rare chance that you do there is a module to interface with the<br>interpreter's garbage collector.  File descriptors, network connections<br>and the like are usually handled either with an explicit .close() on the<br>object or by implementing/using context managers[1]. And of course if<br>you don't do so yourself then when the interpreter exits the OS closes<br>them implicitly.<br><br>1. <a href="http://docs.python.org/library/stdtypes.html#context-manager-types" target="_blank">http://docs.python.org/library/stdtypes.html#context-manager-types</a><br><br>--<br><a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br></div></div></div><br>



      </body></html>