Destructors in Python
Constructors in Python
Destructors are called when an object gets destroyed. In Python, destructors are not needed as much as in C++ because Python has a garbage collector that handles memory management automatically.
The __del__() method is a known as a destructor method in Python. It is called when all references to the object have been deleted i.e when an object is garbage collected.
Syntax of destructor declaration :
def __del__(self): # body of destructor
Note : A reference to objects is also deleted when the object goes out of reference or when the program ends.
Example 1 : Here is the simple example of destructor. By using del keyword we deleted the all references of object ‘obj’, therefore destructor invoked automatically.
Python3
Employee created. Destructor called, Employee deleted.
Note : The destructor was called after the program ended or when all the references to object are deleted i.e when the reference count becomes zero, not when object went out of scope.
Example 2: This example gives the explanation of above-mentioned note. Here, notice that the destructor is called after the ‘Program End…’ printed.
Python3
Calling Create_obj() function. Making Object. Employee created function end. Program End. Destructor called
Example 3: Now, consider the following example :
Python3
In this example when the function fun() is called, it creates an instance of class B which passes itself to class A, which then sets a reference to class B and resulting in a circular reference.
Generally, Python’s garbage collector which is used to detect these types of cyclic references would remove it but in this example the use of custom destructor marks this item as “uncollectable”.
Simply, it doesn’t know the order in which to destroy the objects, so it leaves them. Therefore, if your instances are involved in circular references they will live in memory for as long as the application run.
NOTE : The problem mentioned in example 3 is resolved in newer versions of python, but it still exists in version < 3.4 .
Example: Destruction in recursion
In Python, you can define a destructor for a class using the __del__() method. This method is called automatically when the object is about to be destroyed by the garbage collector. Here’s an example of how to use a destructor in a recursive function:
Python
('Recursive function initialized with n =', 5) ('Running recursive function with n =', 5) ('Running recursive function with n =', 4) ('Running recursive function with n =', 3) ('Running recursive function with n =', 2) ('Running recursive function with n =', 1) Recursive function object destroyed
In this example, we define a class RecursiveFunction with an __init__() method that takes in a parameter n. This parameter is stored as an attribute of the object.
We also define a run() method that takes in an optional parameter n. If n is not provided, it defaults to the value of self.n. The run() method runs a recursive function that prints a message to the console and calls itself with n-1.
We define a destructor using the __del__() method, which simply prints a message to the console indicating that the object has been destroyed.
We create an object of the class RecursiveFunction with n set to 5, and call the run() method. This runs the recursive function, printing a message to the console for each call.
Finally, we destroy the object using the del statement. This triggers the destructor, which prints a message to the console indicating that the object has been destroyed.
Note that in this example, the recursive function will continue running until n reaches 0. When n is 0, the function will return and the object will be destroyed by the garbage collector. The destructor will then be called automatically.
Advantages of using destructors in Python:
- Automatic cleanup: Destructors provide automatic cleanup of resources used by an object when it is no longer needed. This can be especially useful in cases where resources are limited, or where failure to clean up can lead to memory leaks or other issues.
- Consistent behavior: Destructors ensure that an object is properly cleaned up, regardless of how it is used or when it is destroyed. This helps to ensure consistent behavior and can help to prevent bugs and other issues.
- Easy to use: Destructors are easy to implement in Python, and can be defined using the __del__() method.
- Supports object-oriented programming: Destructors are an important feature of object-oriented programming, and can be used to enforce encapsulation and other principles of object-oriented design.
- Helps with debugging: Destructors can be useful for debugging, as they can be used to trace the lifecycle of an object and determine when it is being destroyed.
Overall, destructors are an important feature of Python and can help to ensure that objects are properly cleaned up and resources are not wasted. They are easy to use and can be useful for enforcing encapsulation and other principles of object-oriented design.
What is the __del__ method and how do I call it?
I saw a class in which a __del__ method is defined. This method is used to destroy an instance of the class. However, I cannot find a place where this method is used. How is this method used? Like that: obj1.del() ?. How do I call the __del__ method?
For readers: Check out this video by mCoding, which explains how __del__ works and provides some alternatives to it.
@InSync «For readers: Check out this video by mCoding, . » — well, rather for those who can’t read 😉 (emphasis mine)
@InSync I meant analphabets (functional or otherwise), not people with visual disabilities. But yes, you are right, there are several technologies that can help people suffering from both conditions.
5 Answers 5
__del__ is a finalizer. It is called when an object is garbage collected which happens at some point after all references to the object have been deleted.
In a simple case this could be right after you say del x or, if x 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 an 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 to the finally clause of a try statement or to a context manager in a with statement. However, there are valid use cases for __del__ : e.g. if an object X references Y and also keeps a copy of Y reference in a global cache ( cache[‘X -> Y’] = Y ) then it would be polite for X.__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 only do so if you know it can be called twice. Or, as a last resort, you can redefine this method using
type(x).__del__ = my_safe_cleanup_method
CPython implementation detail: CPython currently uses a reference-counting scheme with (optional) delayed detection of cyclically linked garbage, which collects most objects as soon as they become unreachable [. ] Other implementations act differently and CPython may change.