Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use __del__ in a reliable way?

I have learned that python does not guarantee that __del__ is called whenever an object is deleted. In other words, del x does not necessarily invoke its destructor x.__del__().

If I want to ensure proper object cleanup, I should use a context manager (in a with statement).

I know it's stupid, but for a couple of reasons (please don't ask why) I am tied to a system with Python 2.4; therefore context managers are out of question (they were introduced in Python 2.5)

So I need a an alternative solution, and hence my question: are there best practices that would help me to use __del__ reliably? I am thinking in the direction of "if python provides such functionality, there must be a way it can be efficiently used (I'm just to stupid to figure out how)",,,

Or I am just being naive, should forget about __del__ and move on to a completely different approach?

like image 677
E.Z. Avatar asked Apr 27 '12 14:04

E.Z.


People also ask

What is the use of __ del __ in Python?

In Python, the __del__() method is referred to as a destructor method. It is called after an object's garbage collection occurs, which happens after all references to the item have been destroyed. Student table created. Destructor called, Student table deleted.

What is __ del __ method?

The __del__() method is a known as a destructor method. It is called when an object is garbage collected which happens after all references to the object have been deleted.

Which keyword is used to invoke __ del __ method?

__del__ is a destructor method which is called as soon as all references of the object are deleted i.e when an object is garbage collected. Example: Here is the simple example of destructor. By using del keyword we deleted the all references of object 'obj', therefore destructor invoked automatically.

How do you call a destructor in Python manually?

The __del__() function is used as the destructor function in Python. The user can call the __del__() function when all the references of the object have been deleted, and it becomes garbage collected.


2 Answers

In short: No, there is no way to ensure it gets called.

The answer is to implement context managers yourself. A with statement roughly translates to:

x.__enter__()
try:
    ...
finally:
    x.__exit__()

So just do it manually. It is a little more complex than that, so I recommend reading PEP 343 to fully understand how context managers work.

One option is to call your cleaning up function close(), and then in future versions of python, people can easily use contextlib.closing to turn it into a real context manager.

like image 112
Gareth Latty Avatar answered Sep 28 '22 06:09

Gareth Latty


Instead of __del__, give your class a method called something like close, then call that explicitly:

foo = Foo()
try:
    foo.do_interesting_stuff()
finally:
    foo.close()

For extra safety and forward-compatibility, have __exit__ and __del__ call close as well.

like image 24
Fred Foo Avatar answered Sep 28 '22 08:09

Fred Foo