Thanks; I'll give it a try.<br><br><div><span class="gmail_quote">On 8/22/07, <b class="gmail_sendername">Nils Oliver Kröger</b> <<a href="mailto:NO_Kroeger@gmx.de">NO_Kroeger@gmx.de</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
-----BEGIN PGP SIGNED MESSAGE-----<br>Hash: SHA1<br><br>Robert Dailey schrieb:<br>> Hi,<br>><br>> I'm wondering where the most appropriate location is to cleanup class<br>> objects. For example, i have a file handle as an instance attribute in one
<br>> of my classes and I need to call f.close() on it when the class object falls<br>> out of scope. Any ideas? I've tried __del__() but I don't remember this<br>> working for some reason. I might try it again later just to be positive.
<br><br>__del__(self) is the perfectly right place for such cleanup ... it gets<br>called once your instance is either deleted explicitly by you or it's<br>handled by the garbage collector when there are no more references.
<br><br>The possible problem why this did not work four you is, that the<br>destruction by the garbage collector cannot be predicted ... it may well<br>take some time. If you try to open the same file from another class<br>
before yours gets cleaned you run into trouble.<br><br>If you want to "reuse" the file, you will have to delete your classes<br>instance explicitly using the del statement ... this will also call the<br>destructor.
<br><br>The below code works fine:<br><br>    def __del__( self ):<br>        self._file.close()<br>        print "File closed"<br><br>Hope that helps ... Nils<br>-----BEGIN PGP SIGNATURE-----<br>Version: GnuPG 
v1.4.6 (MingW32)<br>Comment: Using GnuPG with Mozilla - <a href="http://enigmail.mozdev.org">http://enigmail.mozdev.org</a><br><br>iD8DBQFGzISBzvGJy8WEGTcRAiOwAJ94fJza4/GVQsFmbXwsP8kdvQjV5wCfQktw<br>F/zPJAw0ayjYe5MGxPR1YqI=
<br>=4Hl6<br>-----END PGP SIGNATURE-----<br>--<br><a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br></blockquote></div><br>