I am curious about the details of __del__
in python, when and why it should be used and what it shouldn't be used for. I've learned the hard way that it is not really like what one would naively expected from a destructor, in that it is not the opposite of __new__
/ __init__
.
class Foo(object):
def __init__(self):
self.bar = None
def open(self):
if self.bar != 'open':
print 'opening the bar'
self.bar = 'open'
def close(self):
if self.bar != 'closed':
print 'closing the bar'
self.bar = 'close'
def __del__(self):
self.close()
if __name__ == '__main__':
foo = Foo()
foo.open()
del foo
import gc
gc.collect()
I saw in the documentation that it is not guaranteed __del__()
methods are called for objects that still exist when the interpreter exits.
- how can it be guaranteed that for any
Foo
instances existing when the interpreter exits, the bar is closed? - in the code snippet above does the bar get closed on
del foo
or ongc.collect()
... or neither? if you want finer control of those details (e.g. the bar should be closed when the object is unreferenced) what is the usual way to implement that? - when
__del__
is called is it guaranteed that__init__
has already been called? what about if the__init__
raised?
In general, to make sure something happens no matter what, you use
finally
blocks will be run whether or not there is an error in thetry
block, and whether or not there is an error in any error handling that takes place inexcept
blocks. If you don't handle an exception that is raised, it will still be raised after thefinally
block is excecuted.The general way to make sure a file is closed is to use a "context manager".
http://docs.python.org/reference/datamodel.html#context-managers
This will automatically close
f
.For your question #2,
bar
gets closed on immediately when it's reference count reaches zero, so ondel foo
if there are no other references.Objects are NOT created by
__init__
, they're created by__new__
.http://docs.python.org/reference/datamodel.html#object.new
When you do
foo = Foo()
two things are actually happening, first a new object is being created,__new__
, then it is being initialized,__init__
. So there is no way you could possibly calldel foo
before both those steps have taken place. However, if there is an error in__init__
,__del__
will still be called because the object was actually already created in__new__
.Edit: Corrected when deletion happens if a reference count decreases to zero.
The way to close resources are context managers, aka the
with
statement:output:
2) Python's objects get deleted when their reference count is 0. In your example the
del foo
removes the last reference so__del__
is called instantly. The GC has no part in this.output:
The
gc
has nothing to do with deleting your and most other objects. It's there to clean up when simple reference counting does not work, because of self-references or circular references:output:
3) Lets see:
gives:
Objects are created with
__new__
then passed to__init__
asself
. After a exception in__init__
, the object will typically not have a name (ie thef =
part isn't run) so their ref count is 0. This means that the object is deleted normally and__del__
is called.Perhaps you are looking for a context manager?
__del__()
gets called when the number of references to an object hits 0 while the VM is still running. This may be caused by the GC.__init__()
raises an exception then the object is assumed to be incomplete and__del__()
won't be invoked.