I am reading a code. There is a class in which __del__
method is defined. I figured out that this method is used to destroy an instance of the class. However, I cannot find a place where this method is used. The main reason for that is that I do not know how this method is used, probably not like that: obj1.del()
. So, my questions is how to call the __del__
method? Thank you for any help.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
The
__del__
method, it will be called when the object is garbage collected. Note that it isn't necessarily guaranteed to be called though. The following code by itself won't necessarily do it:The reason being that
del
just decrements the reference count by one. If something else has a reference to the object,__del__
won't get called.There are a few caveats to using
__del__
though. Generally, they usually just aren't very useful. It sounds to me more like you want to use a close method or maybe a with statement.See the python documentation on
__del__
methods.One other thing to note:
__del__
methods can inhibit garbage collection if overused. In particular, a circular reference that has more than one object with a__del__
method won't get garbage collected. This is because the garbage collector doesn't know which one to call first. See the documentation on the gc module for more info.__del__
is a destructor. It is called when an object is garbage collected which happens after all references to the object have been deleted.In a simple case this could be right after you say
del x
or, ifx
is a local variable, after the function ends. In particular, unless there are circular references, CPython (the standard Python implementation) will garbage collect immediately.However, this is the implementation detail of CPython. The only required property of Python garbage collection is that it happens after all references have been deleted, so this might not necessary happen right after and might not happen at all.
Even more, variables can live for a long time for many reasons, e.g. a propagating exception or module introspection can keep variable reference count greater than 0. Also, variable can be a part of cycle of references — CPython with garbage collection turned on breaks most, but not all, such cycles, and even then only periodically.
Since you have no guarantee it's executed, one should never put the code that you need to be run into
__del__()
— instead, this code belongs tofinally
clause of thetry
block or to a context manager in awith
statement. However, there are valid use cases for__del__
: e.g. if an objectX
referencesY
and also keeps a copy ofY
reference in a globalcache
(cache['X -> Y'] = Y
) then it would be polite forX.__del__
to also delete the cache entry.If you know that the destructor provides (in violation of the above guideline) a required cleanup, you might want to call it directly, since there is nothing special about it as a method:
x.__del__()
. Obviously, you should you do so only if you know that it doesn't mind to be called twice. Or, as a last resort, you can redefine this method usingI wrote up the answer for another question, though this is a more accurate question for it.
How do constructors and destructors work?
Here is a slightly opinionated answer.
Don't use
__del__
. This is not C++ or a language built for destructors. The__del__
method really should be gone in Python 3.x, though I'm sure someone will find a use case that makes sense. If you need to use__del__
, be aware of the basic limitations per http://docs.python.org/reference/datamodel.html:__del__
is called when the garbage collector happens to be collecting the objects, not when you lose the last reference to an object and not when you executedel object
.__del__
is responsible for calling any__del__
in a superclass, though it is not clear if this is in method resolution order (MRO) or just calling each superclass.__del__
means that the garbage collector gives up on detecting and cleaning any cyclic links, such as losing the last reference to a linked list. You can get a list of the objects ignored from gc.garbage. You can sometimes use weak references to avoid the cycle altogether. This gets debated now and then: see http://mail.python.org/pipermail/python-ideas/2009-October/006194.html.__del__
function can cheat, saving a reference to an object, and stopping the garbage collection.__del__
are ignored.__del__
complements__new__
far more than__init__
. This gets confusing. See http://www.algorithm.co.il/blogs/programming/python-gotchas-1-del-is-not-the-opposite-of-init/ for an explanation and gotchas.__del__
is not a "well-loved" child in Python. You will notice that sys.exit() documentation does not specify if garbage is collected before exiting, and there are lots of odd issues. Calling the__del__
on globals causes odd ordering issues, e.g., http://bugs.python.org/issue5099. Should__del__
called even if the__init__
fails? See http://mail.python.org/pipermail/python-dev/2000-March/thread.html#2423 for a long thread.But, on the other hand:
__del__
means you do not forget to call a close statement. See http://eli.thegreenplace.net/2009/06/12/safely-using-destructors-in-python/ for a pro__del__
viewpoint. This is usually about freeing ctypes or some other special resource.And my pesonal reason for not liking the
__del__
function.__del__
it devolves into thirty messages of confusion.So, find a reason not to use
__del__
.The
__del__
method (note spelling!) is called when your object is finally destroyed. Technically speaking (in cPython) that is when there are no more references to your object, ie when it goes out of scope.If you want to delete your object and thus call the
__del__
method usewhich will delete the object (provided there weren't any other references to it).
I suggest you write a small class like this
And investigate in the python interpreter, eg
Note that jython and ironpython have different rules as to exactly when the object is deleted and
__del__
is called. It isn't considered good practice to use__del__
though because of this and the fact that the object and its environment may be in an unknown state when it is called. It isn't absolutely guaranteed__del__
will be called either - the interpreter can exit in various ways without deleteting all objects.