{"id":25530647,"date":"2022-11-19T16:48:58","date_gmt":"2022-11-19T11:18:58","guid":{"rendered":"https:\/\/entri.app\/blog\/?p=25530647"},"modified":"2023-04-27T10:01:13","modified_gmt":"2023-04-27T04:31:13","slug":"complete-understanding-of-destructors-in-python-2","status":"publish","type":"post","link":"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/","title":{"rendered":"Complete Understanding of Destructors in Python"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_79_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69e634aa79a48\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69e634aa79a48\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#Destructor_is_a_special_method_that_is_called_when_an_object_gets_destroyed_On_the_other_hand_a_constructor_is_used_to_create_and_initialize_an_object_of_a_class\" >Destructor is a special method that is called when an object gets destroyed. On the other hand, a constructor is used to create and initialize an object of a class.<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#What_is_Destructor_in_Python\" >What is Destructor in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#Create_Destructor_using_the_del_Method\" >Create Destructor using the\u00a0__del__()\u00a0Method<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#Important_Points_to_Remember_about_Destructor\" >Important Points to Remember about Destructor<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#Cases_when_Destructor_doesnt_work_Correctly\" >Cases when Destructor doesn\u2019t work Correctly<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<header class=\"entry-header\">\n<h2 class=\"entry-title\"><span class=\"ez-toc-section\" id=\"Destructor_is_a_special_method_that_is_called_when_an_object_gets_destroyed_On_the_other_hand_a_constructor_is_used_to_create_and_initialize_an_object_of_a_class\"><\/span><span style=\"color: #333333; font-size: 15px;\">Destructor is a special method that is called when an object gets destroyed. On the other hand, a constructor is used to create and initialize an object of a class.<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<\/header>\n<div class=\"entry-content\">\n<p>After reading this article, you will learn:<\/p>\n<ul>\n<li>How create a destructor in Python<\/li>\n<li>The use of<code> __del__()<\/code>\u00a0method<\/li>\n<li>Wokring of a destructor<\/li>\n<\/ul>\n<p><a href=\"https:\/\/entri.app\/course\/python-programming-course\/\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-25520910 size-full\" src=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Square.png\" alt=\"Python and Machine Learning Square\" width=\"345\" height=\"345\" srcset=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Square.png 345w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Square-300x300.png 300w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Square-150x150.png 150w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Square-24x24.png 24w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Square-48x48.png 48w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Square-96x96.png 96w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Square-75x75.png 75w\" sizes=\"auto, (max-width: 345px) 100vw, 345px\" \/><\/a><\/p>\n<div class=\"wp-block-yoast-seo-table-of-contents yoast-table-of-contents\"><\/div>\n<h2 id=\"h-what-is-destructor-in-python\"><span class=\"ez-toc-section\" id=\"What_is_Destructor_in_Python\"><\/span><strong>What is Destructor in Python?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In\u00a0object-oriented programming,\u00a0A\u00a0destructor is called when an\u00a0object\u00a0is deleted or destroyed. Destructor is used to perform the clean-up activity before destroying the object, such as closing database connections or filehandle.<\/p>\n<p>Python has a garbage collector that handles memory management automatically. For example, it cleans up the memory when an object goes out of scope.<\/p>\n<p>But it\u2019s not just memory that has to be freed when an object is destroyed. We\u00a0must release or close the other resources object were using, such as open files, database connections, cleaning up the buffer or cache.\u00a0To perform all those cleanup tasks we use destructor in Python.<\/p>\n<p>The destructor is the reverse of the\u00a0constructor. The constructor is used to initialize objects, while the destructor is used to delete or destroy the object that releases the resource occupied by the object.<\/p>\n<p>In Python, destructor is not called manually but completely automatic.\u00a0destructor gets called in the following two cases<\/p>\n<ul>\n<li>When an object goes out of scope or<\/li>\n<li>The reference counter of the object reaches 0.<\/li>\n<\/ul>\n<h4 style=\"text-align: center;\"><strong><span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;\\&quot;Ready to take your python skills to the next level? Sign up for a free demo today!\\&quot;&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1061443,&quot;3&quot;:{&quot;1&quot;:0},&quot;4&quot;:{&quot;1&quot;:2,&quot;2&quot;:16316664},&quot;9&quot;:0,&quot;12&quot;:0,&quot;15&quot;:&quot;Arial&quot;,&quot;16&quot;:10,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/entri.app\/course\/python-programming-course\/\"><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/course\/python-programming-course\/\" target=\"_blank\" rel=\"noopener\">&#8220;Ready to take your python skills to the next level? Sign up for a free demo today!&#8221;<\/a><\/span><\/strong><\/h4>\n<p>In Python, The special method\u00a0<code>__del__()<\/code>\u00a0is used to define a destructor.\u00a0For example, when we execute\u00a0<code>del object_name<\/code>\u00a0destructor gets called automatically and the object gets garbage collected.<\/p>\n<div id=\"AdThrive_Content_3_desktop\" class=\"adthrive-ad adthrive-content adthrive-content-1 adthrive-ad-cls\" data-google-query-id=\"CKPZ596pzvgCFVABcgodv_gJ-A\">\n<div id=\"google_ads_iframe_\/18190176,21948912187\/AdThrive_Content_3\/5e73c35f319b6675881fa748_0__container__\"><\/div>\n<\/div>\n<div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-10113 perfmatters-lazy loaded\" src=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/07\/python_destructor_to_destroy_object.png\" srcset=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/07\/python_destructor_to_destroy_object.png 642w, https:\/\/pynative.com\/wp-content\/uploads\/2021\/07\/python_destructor_to_destroy_object-295x300.png 295w\" alt=\"Python destructor to destroy an object\" width=\"642\" height=\"652\" data-src=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/07\/python_destructor_to_destroy_object.png\" data-srcset=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/07\/python_destructor_to_destroy_object.png 642w, https:\/\/pynative.com\/wp-content\/uploads\/2021\/07\/python_destructor_to_destroy_object-295x300.png 295w\" data-sizes=\"(max-width: 642px) 100vw, 642px\" data-was-processed=\"true\" \/><figcaption>Python destructor to destroy an object<\/figcaption><\/figure>\n<\/div>\n<div id=\"AdThrive_Content_4_desktop\" class=\"adthrive-ad adthrive-content adthrive-content-1 adthrive-ad-cls\" data-google-query-id=\"COrI6N6pzvgCFdl0fQodSWgGiw\">\n<div id=\"google_ads_iframe_\/18190176,21948912187\/AdThrive_Content_4\/5e73c35f319b6675881fa748_0__container__\"><\/div>\n<\/div>\n<h2 id=\"h-create-destructor-using-the-__del__-method\"><span class=\"ez-toc-section\" id=\"Create_Destructor_using_the_del_Method\"><\/span><strong>Create Destructor using the\u00a0<code>__del__()<\/code>\u00a0Method<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The magic method\u00a0<code>__del__()<\/code>\u00a0is used as the destructor in Python. The\u00a0<code>__del__()<\/code>\u00a0method will be implicitly invoked when all references to the object have been deleted, i.e., is when an object is eligible for the garbage collector.<\/p>\n<p>This method is automatically called by Python when the instance is about to be destroyed. It is also called a finalizer or (improperly) a destructor.<\/p>\n<p><strong>Syntax of destructor declaration<\/strong><\/p>\n<div class=\"code-toolbar\">\n<pre id=\"syntax\" class=\"wp-block-code language-python\"><code class=\" language-python\"><span class=\"token keyword\">def<\/span> <span class=\"token function\">__del__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>\r\n    <span class=\"token comment\"># body of a destructor<\/span><\/code><\/pre>\n<\/div>\n<div id=\"AdThrive_Content_5_desktop\" class=\"adthrive-ad adthrive-content adthrive-content-1 adthrive-ad-cls\" data-google-query-id=\"CKrf_d6pzvgCFYkAgwMd4AQFfQ\">\n<div id=\"google_ads_iframe_\/18190176,21948912187\/AdThrive_Content_5\/5e73c35f319b6675881fa748_0__container__\"><\/div>\n<\/div>\n<p>Where,<\/p>\n<ul>\n<li><code>def<\/code>: The keyword is used to define a method.<\/li>\n<li><code>__del__()<\/code>\u00a0Method: It is a reserved method. This method gets called as soon as all references to the object have been deleted<\/li>\n<li><code>self<\/code>: The first argument\u00a0<code>self<\/code>\u00a0refers to the current object.<\/li>\n<\/ul>\n<p><strong>Note<\/strong>: The\u00a0<code>__del__()<\/code>\u00a0method arguments are optional. We can define a destructor with any number of arguments.<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>Let\u2019s see how to create a destructor in Python with a simple example. In this example, we\u2019ll create a Class Student with a destructor. We\u2019ll see: \u2013<\/p>\n<ul>\n<li>How to implement a destructor<\/li>\n<li>how destructor gets executed when we delete the object.<\/li>\n<\/ul>\n<div class=\"code-toolbar\">\n<pre class=\"wp-block-code language-python\"><code class=\" language-python\"><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Student<\/span><span class=\"token punctuation\">:<\/span>\r\n\r\n    <span class=\"token comment\"># constructor<\/span>\r\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">__init__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> name<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>\r\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'Inside Constructor'<\/span><span class=\"token punctuation\">)<\/span>\r\n        self<span class=\"token punctuation\">.<\/span>name <span class=\"token operator\">=<\/span> name\r\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'Object initialized'<\/span><span class=\"token punctuation\">)<\/span>\r\n\r\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">show<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>\r\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'Hello, my name is'<\/span><span class=\"token punctuation\">,<\/span> self<span class=\"token punctuation\">.<\/span>name<span class=\"token punctuation\">)<\/span>\r\n\r\n    <span class=\"token comment\"># destructor<\/span>\r\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">__del__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>\r\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'Inside destructor'<\/span><span class=\"token punctuation\">)<\/span>\r\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'Object destroyed'<\/span><span class=\"token punctuation\">)<\/span>\r\n\r\n<span class=\"token comment\"># create object<\/span>\r\ns1 <span class=\"token operator\">=<\/span> Student<span class=\"token punctuation\">(<\/span><span class=\"token string\">'Emma'<\/span><span class=\"token punctuation\">)<\/span>\r\ns1<span class=\"token punctuation\">.<\/span>show<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>\r\n\r\n<span class=\"token comment\"># delete object<\/span>\r\n<span class=\"token keyword\">del<\/span> s1\r\n<\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><\/div>\n<\/div>\n<\/div>\n<p><strong>Output<\/strong><\/p>\n<pre class=\"wp-block-preformatted\">Inside Constructor\r\nObject initialized\r\n\r\nHello, my name is Emma\r\n\r\nInside destructor\r\nObject destroyed<\/pre>\n<p><strong>Note<\/strong>:<\/p>\n<p>As you can see in the output, the\u00a0<code>__del__()<\/code>\u00a0method get called automatically is called when we deleted the object reference using\u00a0<code>del s1<\/code>.<\/p>\n<div id=\"AdThrive_Content_8_desktop\" class=\"adthrive-ad adthrive-content adthrive-content-1 adthrive-ad-cls\" data-google-query-id=\"CI241d-pzvgCFfKTSwUduiUHSQ\">\n<div id=\"google_ads_iframe_\/18190176,21948912187\/AdThrive_Content_8\/5e73c35f319b6675881fa748_0__container__\">In the above code, we created one object. The <code>s1<\/code>\u00a0is the reference variable that is pointing to the newly created object.<\/div>\n<\/div>\n<p>The destructor has called when the reference to the object is deleted or the reference count for the object becomes zero<\/p>\n<h4 style=\"text-align: center;\"><span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;\\&quot;Experience the power of our web development course with a free demo - enroll now!\\&quot;&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1061379,&quot;3&quot;:{&quot;1&quot;:0},&quot;4&quot;:{&quot;1&quot;:2,&quot;2&quot;:16777215},&quot;12&quot;:0,&quot;15&quot;:&quot;Roboto, RobotoDraft, Helvetica, Arial, sans-serif&quot;,&quot;16&quot;:10,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/entri.app\/course\/python-programming-course\/\"><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/course\/python-programming-course\/\" target=\"_blank\" rel=\"noopener\">&#8220;Experience the power of our web development course with a free demo &#8211; enroll now!&#8221;<\/a><\/span><\/h4>\n<h2 id=\"h-important-points-to-remember-about-destructor\"><span class=\"ez-toc-section\" id=\"Important_Points_to_Remember_about_Destructor\"><\/span><strong>Important Points to Remember about Destructor<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>The\u00a0<code>__del__<\/code>\u00a0method is called for any object when the reference count for that object becomes zero.<\/li>\n<li>The reference count for that object becomes zero when the application ends, or we delete all references manually using the\u00a0<code>del<\/code>\u00a0keyword.<\/li>\n<li>The destructor will not invoke when we delete object reference. It will only invoke when all references to the objects get deleted.<\/li>\n<\/ul>\n<div id=\"AdThrive_Content_9_desktop\" class=\"adthrive-ad adthrive-content adthrive-content-1 adthrive-ad-cls\" data-google-query-id=\"CJOC-d-pzvgCFUY6KwodKwgCPw\">\n<div id=\"google_ads_iframe_\/18190176,21948912187\/AdThrive_Content_9\/5e73c35f319b6675881fa748_0__container__\"><\/div>\n<\/div>\n<div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-10112 perfmatters-lazy loaded\" src=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/07\/working_of_destructor.png\" srcset=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/07\/working_of_destructor.png 738w, https:\/\/pynative.com\/wp-content\/uploads\/2021\/07\/working_of_destructor-300x259.png 300w\" alt=\"Working of destructor\" width=\"738\" height=\"636\" data-src=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/07\/working_of_destructor.png\" data-srcset=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/07\/working_of_destructor.png 738w, https:\/\/pynative.com\/wp-content\/uploads\/2021\/07\/working_of_destructor-300x259.png 300w\" data-sizes=\"(max-width: 738px) 100vw, 738px\" data-was-processed=\"true\" \/><figcaption>Working of destructor<\/figcaption><\/figure>\n<\/div>\n<p><strong>Example<\/strong>:<\/p>\n<p>Let\u2019s understand the above points using the example.<\/p>\n<ul>\n<li>First create object of a student class using\u00a0<code>s1 = student('Emma')<\/code><\/li>\n<li>Next, create a new object reference\u00a0<code>s2<\/code>\u00a0by assigning\u00a0<code>s1<\/code>\u00a0to\u00a0<code>s2<\/code>\u00a0using\u00a0<code>s2=s1<\/code><\/li>\n<li>Now, both reference variables\u00a0<code>s1<\/code>\u00a0and\u00a0<code>s2<\/code>\u00a0point to the same object.<\/li>\n<li>Next, we deleted reference\u00a0<code>s1<\/code><\/li>\n<li>Next, we have added 5 seconds of sleep to the main thread to understand that destructors only invoke when all references to the objects get deleted.<\/li>\n<\/ul>\n<div class=\"code-toolbar\">\n<pre class=\"wp-block-code language-python\"><code class=\" language-python\"><span class=\"token keyword\">import<\/span> time\r\n\r\n<span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Student<\/span><span class=\"token punctuation\">:<\/span>\r\n\r\n    <span class=\"token comment\"># constructor<\/span>\r\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">__init__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> name<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>\r\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'Inside Constructor'<\/span><span class=\"token punctuation\">)<\/span>\r\n        self<span class=\"token punctuation\">.<\/span>name <span class=\"token operator\">=<\/span> name\r\n\r\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">show<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>\r\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'Hello, my name is'<\/span><span class=\"token punctuation\">,<\/span> self<span class=\"token punctuation\">.<\/span>name<span class=\"token punctuation\">)<\/span>\r\n\r\n    <span class=\"token comment\"># destructor<\/span>\r\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">__del__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>\r\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'Object destroyed'<\/span><span class=\"token punctuation\">)<\/span>\r\n\r\n<span class=\"token comment\"># create object<\/span>\r\ns1 <span class=\"token operator\">=<\/span> Student<span class=\"token punctuation\">(<\/span><span class=\"token string\">'Emma'<\/span><span class=\"token punctuation\">)<\/span>\r\n<span class=\"token comment\"># create new reference<\/span>\r\n<span class=\"token comment\"># both reference points to the same object<\/span>\r\ns2 <span class=\"token operator\">=<\/span> s1\r\ns1<span class=\"token punctuation\">.<\/span>show<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>\r\n\r\n<span class=\"token comment\"># delete object reference s1<\/span>\r\n<span class=\"token keyword\">del<\/span> s1\r\n\r\n<span class=\"token comment\"># add sleep and observe the output<\/span>\r\ntime<span class=\"token punctuation\">.<\/span>sleep<span class=\"token punctuation\">(<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">)<\/span>\r\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'After sleep'<\/span><span class=\"token punctuation\">)<\/span>\r\ns2<span class=\"token punctuation\">.<\/span>show<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><\/div>\n<\/div>\n<\/div>\n<div class=\"wp-container-3 wp-block-columns\">\n<div class=\"wp-container-1 wp-block-column\">\n<p><strong>Output<\/strong>:<\/p>\n<pre class=\"wp-block-preformatted\">Inside Constructor\r\nHello, my name is Emma\r\n<\/pre>\n<\/div>\n<div class=\"wp-container-2 wp-block-column\">\n<p><strong>After Sleep<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>After sleep\r\nHello, my name is Emma\r\nObject destroyed<\/code><\/pre>\n<\/div>\n<\/div>\n<ul>\n<li>As you can see in the output\u00a0destructors only invoked when all references to the objects get deleted.<\/li>\n<li>Also, the destructor is executed when the code (application) ends and the object is available for the garbage collector. (I.e., we didn\u2019t delete object reference\u00a0<code>s2<\/code>\u00a0manually using\u00a0<code>del s2<\/code>).<\/li>\n<\/ul>\n<h2 id=\"h-cases-when-destructor-doesn-t-work-correctly\"><span class=\"ez-toc-section\" id=\"Cases_when_Destructor_doesnt_work_Correctly\"><\/span><strong>Cases when Destructor doesn\u2019t work Correctly<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The _<code>_del__<\/code>\u00a0is not a perfect solution to clean up a Python object when it is no longer required. In Python, the destructor behave behaves weirdly and doesn\u2019t execute in the following two cases.<\/p>\n<ol>\n<li>Circular referencing when two objects refer to each other<\/li>\n<li>Exception occured in __init__() method<\/li>\n<\/ol>\n<h4 style=\"text-align: center;\"><span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;\\&quot;Get hands-on with our python course - sign up for a free demo!\\&quot;&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1061443,&quot;3&quot;:{&quot;1&quot;:0},&quot;4&quot;:{&quot;1&quot;:2,&quot;2&quot;:16316664},&quot;9&quot;:0,&quot;12&quot;:0,&quot;15&quot;:&quot;Arial&quot;,&quot;16&quot;:10,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/entri.app\/course\/python-programming-course\/\"><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/course\/python-programming-course\/\" target=\"_blank\" rel=\"noopener\">&#8220;Get hands-on with our python course &#8211; sign up for a free demo!&#8221;<\/a><\/span><\/h4>\n<h3 id=\"h-circular-referencing\"><strong>Circular Referencing<\/strong><\/h3>\n<p>The\u00a0<code>__del()__()<\/code>\u00a0doesn\u2019t work correctly in the case of circular referencing. In circular referencing occurs\u00a0<strong>when two objects refer to each other<\/strong>.<\/p>\n<p>When both objects go out of scope, Python doesn\u2019t know which object to destroy first. So, to avoid any errors, it doesn\u2019t destroy any of them.<\/p>\n<p>In short, it means that the garbage collector does not know the order in which the object should be destroyed, so it doesn\u2019t delete them from memory.<\/p>\n<p>Ideally, the destructor must execute when an object goes out of scope, or its reference count reaches zero.<\/p>\n<p>But the objects involved in this circular reference will remain stored in the memory as long as the application will run.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p>In the below example, ideally, both Vehicle and Car objects must be destroyed by the garbage collector after they go out of scope. Still, because of the circular reference, they remain in memory.<\/p>\n<p>I\u2019d recommend using Python\u2019s\u00a0with statement\u00a0for managing resources that need to be cleaned up.<\/p>\n<div class=\"code-toolbar\">\n<pre class=\"wp-block-code language-python\"><code class=\" language-python\"><span class=\"token keyword\">import<\/span> time\r\n\r\n\r\n<span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Vehicle<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>\r\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">__init__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> <span class=\"token builtin\">id<\/span><span class=\"token punctuation\">,<\/span> car<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>\r\n        self<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">id<\/span> <span class=\"token operator\">=<\/span> <span class=\"token builtin\">id<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token comment\"># saving reference of Car object<\/span>\r\n        self<span class=\"token punctuation\">.<\/span>dealer <span class=\"token operator\">=<\/span> car<span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'Vehicle'<\/span><span class=\"token punctuation\">,<\/span> self<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">id<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">'created'<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n\r\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">__del__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>\r\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'Vehicle'<\/span><span class=\"token punctuation\">,<\/span> self<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">id<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">'destroyed'<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n\r\n\r\n<span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Car<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>\r\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">__init__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> <span class=\"token builtin\">id<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>\r\n        self<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">id<\/span> <span class=\"token operator\">=<\/span> <span class=\"token builtin\">id<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token comment\"># saving Vehicle class object in 'dealer' variable<\/span>\r\n        <span class=\"token comment\"># Sending reference of Car object ('self') for Vehicle object<\/span>\r\n        self<span class=\"token punctuation\">.<\/span>dealer <span class=\"token operator\">=<\/span> Vehicle<span class=\"token punctuation\">(<\/span><span class=\"token builtin\">id<\/span><span class=\"token punctuation\">,<\/span> self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'Car'<\/span><span class=\"token punctuation\">,<\/span> self<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">id<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">'created'<\/span><span class=\"token punctuation\">)<\/span>\r\n\r\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">__del__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>\r\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'Car'<\/span><span class=\"token punctuation\">,<\/span> self<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">id<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">'destroyed'<\/span><span class=\"token punctuation\">)<\/span>\r\n\r\n\r\n<span class=\"token comment\"># create car object<\/span>\r\nc <span class=\"token operator\">=<\/span> Car<span class=\"token punctuation\">(<\/span><span class=\"token number\">12<\/span><span class=\"token punctuation\">)<\/span>\r\n<span class=\"token comment\"># delete car object<\/span>\r\n<span class=\"token keyword\">del<\/span> c\r\n<span class=\"token comment\"># ideally destructor must execute now<\/span>\r\n\r\n<span class=\"token comment\"># to observe the behavior<\/span>\r\ntime<span class=\"token punctuation\">.<\/span>sleep<span class=\"token punctuation\">(<\/span><span class=\"token number\">8<\/span><span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<\/div>\n<p><strong>Output<\/strong>:<\/p>\n<pre class=\"wp-block-preformatted\">Vehicle 12 created\r\nCar 12 created<\/pre>\n<h3 id=\"h-exception-in-__init__-method\">Exception in\u00a0<code>__init__<\/code>\u00a0Method<\/h3>\n<p>In object-oriented programming, A constructor is a special method used to create and initialize an object of a class. using the\u00a0__init__()\u00a0method we can implement a constructor to initialize the object.<\/p>\n<p>In OOP, if any exception occurs in the\u00a0constructor\u00a0while initializing the object, the constructor destroys the object.<\/p>\n<p>Likewise, in Python, if any exception occurs in the\u00a0<strong>init<\/strong>\u00a0method while initializing the object, the method\u00a0<strong>del<\/strong>\u00a0gets called. But actually, an object is not created successfully, and resources are not allocated to it<\/p>\n<p>even though the object was never initialized correctly, the\u00a0<strong>del<\/strong>\u00a0method will try to empty all the resources and, in turn, may lead to another exception.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<div class=\"code-toolbar\">\n<pre class=\"wp-block-code language-python\"><code class=\" language-python\"><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Vehicle<\/span><span class=\"token punctuation\">:<\/span>\r\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">__init__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> speed<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>\r\n        <span class=\"token keyword\">if<\/span> speed <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">240<\/span><span class=\"token punctuation\">:<\/span>\r\n            <span class=\"token keyword\">raise<\/span> Exception<span class=\"token punctuation\">(<\/span><span class=\"token string\">'Not Allowed'<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        self<span class=\"token punctuation\">.<\/span>speed <span class=\"token operator\">=<\/span> speed<span class=\"token punctuation\">;<\/span>\r\n\r\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">__del__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>\r\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'Release resources'<\/span><span class=\"token punctuation\">)<\/span>\r\n\r\n<span class=\"token comment\"># creating an object<\/span>\r\ncar <span class=\"token operator\">=<\/span> Vehicle<span class=\"token punctuation\">(<\/span><span class=\"token number\">350<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token comment\"># to delete the object explicitly<\/span>\r\n<span class=\"token keyword\">del<\/span> car\r\n<\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><\/div>\n<\/div>\n<\/div>\n<p><strong>Output<\/strong>:<\/p>\n<pre class=\"wp-block-preformatted\">Traceback (most recent call last):\r\nRelease resources\r\nException: Not Allowed<\/pre>\n<h2 id=\"h-summary-and-quick-recap\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>In\u00a0object-oriented programming,\u00a0A destructor is called when an object is deleted or destroyed.<\/li>\n<li>Destructor is used to perform the clean-up activity before destroying the object, such as closing database connections or filehandle.<\/li>\n<li>In Python we use\u00a0<code>__del__()<\/code>\u00a0method to perform clean-up task before deleting the object.<\/li>\n<li>The destructor will not invoke when we delete object reference. It will only invoke when all references to the objects get deleted.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><strong>Related Articles<\/strong><\/h4>\n<div class=\"table-responsive wprt_style_display\">\n<div class=\"table-responsive wprt_style_display\">\n<div class=\"table-responsive wprt_style_display\">\n<div class=\"table-responsive wprt_style_display\">\n<table class=\"table\" dir=\"ltr\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<colgroup>\n<col width=\"329\" \/>\n<col width=\"309\" \/><\/colgroup>\n<tbody>\n<tr>\n<td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Kerala PSC VFA Syllabus&quot;}\" data-sheets-hyperlink=\"https:\/\/entri.app\/blog\/kerala-psc-village-field-assistant-vfa-syllabus-exam-pattern\/\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/blog\/step-by-step-guide-for-getting-a-job-as-a-python-developer\/42\" target=\"_blank\" rel=\"noopener\">A Step-by-Step Guide for Getting a Job as a Python Developer<\/a><\/strong><\/td>\n<td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Kerala PSC VFA Mock Test&quot;}\" data-sheets-hyperlink=\"https:\/\/entri.app\/blog\/kerala-psc-vfa-free-mock-test\/\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/blog\/why-python-is-used-for-data-science\/\" target=\"_blank\" rel=\"noopener\">Why Python Is Used For Data Science?<\/a><\/strong><\/td>\n<\/tr>\n<tr>\n<td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Kerala PSC VFA Exam Date&quot;}\" data-sheets-hyperlink=\"https:\/\/entri.app\/blog\/kerala-psc-vfa-exam-date\/\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/blog\/how-to-learn-python-at-home\/\" target=\"_blank\" rel=\"noopener\">How To Learn Python At Home?<\/a><\/strong><\/td>\n<td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Kerala PSC VFA Video Course&quot;}\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/blog\/constructors-and-destructors-in-python\/\" target=\"_blank\" rel=\"noopener\">What are Constructors and Destructors in Python?<\/a><\/strong><\/td>\n<\/tr>\n<tr>\n<td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Kerala PSC VFA Application Form&quot;}\" data-sheets-hyperlink=\"https:\/\/entri.app\/blog\/kerala-psc-vfa-apply-online\/\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/blog\/python-online-course\/\" target=\"_blank\" rel=\"noopener\">Best Online Python Course with Certificate<\/a><\/strong><\/td>\n<td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Kerala PSC VFA Study Materials&quot;}\" data-sheets-hyperlink=\"https:\/\/entri.app\/blog\/kerala-psc-vfa-study-material\/\"><a href=\"https:\/\/entri.app\/blog\/switch-case-in-python-switch-function\/\"><strong>Switch Case in Python<\/strong><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Destructor is a special method that is called when an object gets destroyed. On the other hand, a constructor is used to create and initialize an object of a class. After reading this article, you will learn: How create a destructor in Python The use of __del__()\u00a0method Wokring of a destructor What is Destructor in [&hellip;]<\/p>\n","protected":false},"author":111,"featured_media":25536069,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[802,1888],"tags":[],"class_list":["post-25530647","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-articles","category-python-programming"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Complete Understanding of Destructors in Python - Entri Blog<\/title>\n<meta name=\"description\" content=\"Destructor is a special method that is called when an object gets destroyed. On the other hand, a constructor is used to create and initialize an object of a class.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Complete Understanding of Destructors in Python - Entri Blog\" \/>\n<meta property=\"og:description\" content=\"Destructor is a special method that is called when an object gets destroyed. On the other hand, a constructor is used to create and initialize an object of a class.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Entri Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/entri.me\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-19T11:18:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-27T04:31:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/07\/Complete-Understanding-of-Destructors-in-Python.png\" \/>\n\t<meta property=\"og:image:width\" content=\"820\" \/>\n\t<meta property=\"og:image:height\" content=\"615\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Feeba Mahin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@entri_app\" \/>\n<meta name=\"twitter:site\" content=\"@entri_app\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Feeba Mahin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/\"},\"author\":{\"name\":\"Feeba Mahin\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/person\/f036dab84abae3dcc9390a1110d95d36\"},\"headline\":\"Complete Understanding of Destructors in Python\",\"datePublished\":\"2022-11-19T11:18:58+00:00\",\"dateModified\":\"2023-04-27T04:31:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/\"},\"wordCount\":1187,\"commentCount\":6,\"publisher\":{\"@id\":\"https:\/\/entri.app\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/07\/Complete-Understanding-of-Destructors-in-Python.png\",\"articleSection\":[\"Articles\",\"Python Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/\",\"url\":\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/\",\"name\":\"Complete Understanding of Destructors in Python - Entri Blog\",\"isPartOf\":{\"@id\":\"https:\/\/entri.app\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/07\/Complete-Understanding-of-Destructors-in-Python.png\",\"datePublished\":\"2022-11-19T11:18:58+00:00\",\"dateModified\":\"2023-04-27T04:31:13+00:00\",\"description\":\"Destructor is a special method that is called when an object gets destroyed. On the other hand, a constructor is used to create and initialize an object of a class.\",\"breadcrumb\":{\"@id\":\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#primaryimage\",\"url\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/07\/Complete-Understanding-of-Destructors-in-Python.png\",\"contentUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/07\/Complete-Understanding-of-Destructors-in-Python.png\",\"width\":820,\"height\":615,\"caption\":\"Complete Understanding of Destructors in Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/entri.app\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Programming\",\"item\":\"https:\/\/entri.app\/blog\/category\/python-programming\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Complete Understanding of Destructors in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/entri.app\/blog\/#website\",\"url\":\"https:\/\/entri.app\/blog\/\",\"name\":\"Entri Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/entri.app\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/entri.app\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/entri.app\/blog\/#organization\",\"name\":\"Entri App\",\"url\":\"https:\/\/entri.app\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png\",\"contentUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png\",\"width\":989,\"height\":446,\"caption\":\"Entri App\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/entri.me\/\",\"https:\/\/x.com\/entri_app\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/person\/f036dab84abae3dcc9390a1110d95d36\",\"name\":\"Feeba Mahin\",\"url\":\"https:\/\/entri.app\/blog\/author\/feeba123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Complete Understanding of Destructors in Python - Entri Blog","description":"Destructor is a special method that is called when an object gets destroyed. On the other hand, a constructor is used to create and initialize an object of a class.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/","og_locale":"en_US","og_type":"article","og_title":"Complete Understanding of Destructors in Python - Entri Blog","og_description":"Destructor is a special method that is called when an object gets destroyed. On the other hand, a constructor is used to create and initialize an object of a class.","og_url":"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/","og_site_name":"Entri Blog","article_publisher":"https:\/\/www.facebook.com\/entri.me\/","article_published_time":"2022-11-19T11:18:58+00:00","article_modified_time":"2023-04-27T04:31:13+00:00","og_image":[{"width":820,"height":615,"url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/07\/Complete-Understanding-of-Destructors-in-Python.png","type":"image\/png"}],"author":"Feeba Mahin","twitter_card":"summary_large_image","twitter_creator":"@entri_app","twitter_site":"@entri_app","twitter_misc":{"Written by":"Feeba Mahin","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#article","isPartOf":{"@id":"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/"},"author":{"name":"Feeba Mahin","@id":"https:\/\/entri.app\/blog\/#\/schema\/person\/f036dab84abae3dcc9390a1110d95d36"},"headline":"Complete Understanding of Destructors in Python","datePublished":"2022-11-19T11:18:58+00:00","dateModified":"2023-04-27T04:31:13+00:00","mainEntityOfPage":{"@id":"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/"},"wordCount":1187,"commentCount":6,"publisher":{"@id":"https:\/\/entri.app\/blog\/#organization"},"image":{"@id":"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#primaryimage"},"thumbnailUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/07\/Complete-Understanding-of-Destructors-in-Python.png","articleSection":["Articles","Python Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/","url":"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/","name":"Complete Understanding of Destructors in Python - Entri Blog","isPartOf":{"@id":"https:\/\/entri.app\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#primaryimage"},"image":{"@id":"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#primaryimage"},"thumbnailUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/07\/Complete-Understanding-of-Destructors-in-Python.png","datePublished":"2022-11-19T11:18:58+00:00","dateModified":"2023-04-27T04:31:13+00:00","description":"Destructor is a special method that is called when an object gets destroyed. On the other hand, a constructor is used to create and initialize an object of a class.","breadcrumb":{"@id":"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#primaryimage","url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/07\/Complete-Understanding-of-Destructors-in-Python.png","contentUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/07\/Complete-Understanding-of-Destructors-in-Python.png","width":820,"height":615,"caption":"Complete Understanding of Destructors in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/entri.app\/blog\/complete-understanding-of-destructors-in-python-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/entri.app\/blog\/"},{"@type":"ListItem","position":2,"name":"Python Programming","item":"https:\/\/entri.app\/blog\/category\/python-programming\/"},{"@type":"ListItem","position":3,"name":"Complete Understanding of Destructors in Python"}]},{"@type":"WebSite","@id":"https:\/\/entri.app\/blog\/#website","url":"https:\/\/entri.app\/blog\/","name":"Entri Blog","description":"","publisher":{"@id":"https:\/\/entri.app\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/entri.app\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/entri.app\/blog\/#organization","name":"Entri App","url":"https:\/\/entri.app\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png","contentUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png","width":989,"height":446,"caption":"Entri App"},"image":{"@id":"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/entri.me\/","https:\/\/x.com\/entri_app"]},{"@type":"Person","@id":"https:\/\/entri.app\/blog\/#\/schema\/person\/f036dab84abae3dcc9390a1110d95d36","name":"Feeba Mahin","url":"https:\/\/entri.app\/blog\/author\/feeba123\/"}]}},"_links":{"self":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25530647","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/comments?post=25530647"}],"version-history":[{"count":6,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25530647\/revisions"}],"predecessor-version":[{"id":25559283,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25530647\/revisions\/25559283"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/media\/25536069"}],"wp:attachment":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/media?parent=25530647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/categories?post=25530647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/tags?post=25530647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}