Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Class destruction

2 views
Skip to first unread message

Nils Oliver Kröger

unread,
Aug 22, 2007, 2:46:26 PM8/22/07
to pytho...@python.org
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Robert Dailey schrieb:
> Hi,
>
> I'm wondering where the most appropriate location is to cleanup class
> objects. For example, i have a file handle as an instance attribute in one
> of my classes and I need to call f.close() on it when the class object falls
> out of scope. Any ideas? I've tried __del__() but I don't remember this
> working for some reason. I might try it again later just to be positive.

__del__(self) is the perfectly right place for such cleanup ... it gets
called once your instance is either deleted explicitly by you or it's
handled by the garbage collector when there are no more references.

The possible problem why this did not work four you is, that the
destruction by the garbage collector cannot be predicted ... it may well
take some time. If you try to open the same file from another class
before yours gets cleaned you run into trouble.

If you want to "reuse" the file, you will have to delete your classes
instance explicitly using the del statement ... this will also call the
destructor.

The below code works fine:

def __del__( self ):
self._file.close()
print "File closed"

Hope that helps ... Nils
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGzISBzvGJy8WEGTcRAiOwAJ94fJza4/GVQsFmbXwsP8kdvQjV5wCfQktw
F/zPJAw0ayjYe5MGxPR1YqI=
=4Hl6
-----END PGP SIGNATURE-----

Marshall T. Vandegrift

unread,
Aug 22, 2007, 7:06:51 PM8/22/07
to pytho...@python.org
Nils Oliver Kröger <NO_Kr...@gmx.de> writes:

> If you want to "reuse" the file, you will have to delete your classes
> instance explicitly using the del statement ... this will also call
> the destructor.

Sometimes, but not always. The `del' statement simple removes the
reference to the instance and decrements its reference count. The
__del__() routine for the instance still only gets called whenever the
object is actually garbage collected. Furthermore, the Python Reference
Manual notes that:

Circular references which are garbage are detected when the option
cycle detector is enabled (it's on by default), but can only be
cleaned up if there are no Python-level __del__() methods involved.
[http://docs.python.org/ref/customization.html]

The proper way to handle the case presented by the OP is for the class
to expose a close()-like method and/or -- for use with Python 2.5 and
later -- implement the methods of the context manager protocol
[http://docs.python.org/ref/context-managers.html]. The following code
supports basic usage:

def close(self):
self._file.close()
print "File closed"

def __enter__(self):
return self

def __exit__(self, *exc_info):
self.close()
return False

Then the users of this class can freely do any of:

f = fout(filename)
...
f.close()

with fout(filename) as f:
...

with closing(fout(filename)) as f:
...

HTH,

-Marshall

0 new messages