{"id":162,"date":"2024-01-25T14:05:14","date_gmt":"2024-01-25T14:05:14","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=162"},"modified":"2024-01-25T14:05:15","modified_gmt":"2024-01-25T14:05:15","slug":"python-__del__","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-__del__\/","title":{"rendered":"Python __del__"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about the Python&nbsp;<code>__del__<\/code>&nbsp;special method and understand how it works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python&nbsp;<code>__del__<\/code>&nbsp;method<\/h2>\n\n\n\n<p>In Python, the\u00a0garbage collector\u00a0manages memory automatically. The garbage collector will destroy the objects that are not\u00a0referenced.<\/p>\n\n\n\n<p>If an object implements the&nbsp;<code>__del__<\/code>&nbsp;method, Python calls the&nbsp;<code>__del__<\/code>&nbsp;method right before the garbage collector destroys the object.<\/p>\n\n\n\n<p>However, the garbage collector determines when to destroy the object. Therefore, it determines when the&nbsp;<code>__del__<\/code>&nbsp;method will be called.<\/p>\n\n\n\n<p>The&nbsp;<code>__del__<\/code>&nbsp;is sometimes referred to as a class finalizer. Note that&nbsp;<code>__del__<\/code>&nbsp;is not the destructor because the garbage collector destroys the object, not the&nbsp;<code>__del__<\/code>&nbsp;method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Python __del__ pitfalls<\/h2>\n\n\n\n<p>Python calls the&nbsp;<code>__del__<\/code>&nbsp;method when all object references are gone. And you cannot control it in most cases.<\/p>\n\n\n\n<p>Therefore, you should not use the&nbsp;<code>__del__<\/code>&nbsp;method to clean up the resources. It\u2019s recommended to use the context manager.<\/p>\n\n\n\n<p>If the&nbsp;<code>__del__<\/code>&nbsp;contains references to objects, the garbage collector will also destroy these objects when the&nbsp;<code>__del__<\/code>&nbsp;is called.<\/p>\n\n\n\n<p>If the&nbsp;<code>__del__<\/code>&nbsp;references the global objects, it may create unexpected behaviors.<\/p>\n\n\n\n<p>If an exception occurs inside the&nbsp;<code>__del__<\/code>&nbsp;method, Python does not raise the exception but keeps it silent.<\/p>\n\n\n\n<p>Also, Python sends the exception message to the stderr. Therefore, the main program will be able to be aware of the exceptions during the finalization.<\/p>\n\n\n\n<p>In practice, you\u2019ll rarely use the&nbsp;<code>__del__<\/code>&nbsp;method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python __del__ example<\/h2>\n\n\n\n<p>The following defines a\u00a0<code>Person<\/code>\u00a0class\u00a0with the special\u00a0<code>__del__<\/code>\u00a0method, create a new instance of the\u00a0<code>Person<\/code>, and set it to\u00a0<code>None<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Person: def __init__(self, name, age): self.name = name self.age = age def __del__(self): print('__del__ was called') if __name__ == '__main__': person = Person('John Doe', 23) person = None<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>__del__ was called<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>When we set the&nbsp;<code>person<\/code>&nbsp;object to&nbsp;<code>None<\/code>, the garbage collector destroys it because there is no reference. Therefore, the&nbsp;<code>__del__<\/code>&nbsp;method was called.<\/p>\n\n\n\n<p>If you use the\u00a0<code>del<\/code>\u00a0keyword to delete the\u00a0<code>person<\/code>\u00a0object, the\u00a0<code>__del__<\/code>\u00a0method is also called:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>person = Person('John Doe', 23) del person<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>__del__ was called<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>However, the&nbsp;<code>del<\/code>&nbsp;statement doesn\u2019t cause a call to the&nbsp;<code>__del__<\/code>&nbsp;method if the object has a reference.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn about the Python&nbsp;__del__&nbsp;special method and understand how it works. Introduction to the Python&nbsp;__del__&nbsp;method In Python, the\u00a0garbage collector\u00a0manages memory automatically. The garbage collector will destroy the objects that are not\u00a0referenced. If an object implements the&nbsp;__del__&nbsp;method, Python calls the&nbsp;__del__&nbsp;method right before the garbage collector destroys the object. However, the garbage [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-162","post","type-post","status-publish","format-standard","hentry","category-2-special-methods"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=162"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/162\/revisions"}],"predecessor-version":[{"id":163,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/162\/revisions\/163"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}