The __del__ method of Python's destructor

Source: Internet
Author: User


#!/usr/bin/env python#-*-coding:utf-8-*-# File: Destruction method del.py# author:huxianyong# date:2018/7/9class Foo: # Initialize Party # # # # # # # # # # # # # # # # # # # # # The object is automatically called Def __init__ (Self, name): Print (' __init__ method called ') Self.name = name # destructor # when the object is When deleted, is automatically called and then frees Memory Def __del__ (self): print ("script run ends, free memory") # Create Object f1 = Foo ("Hxy") # Delete Object del f1.name# print (f1.name) # Del F1print (' This should be the last sentence of the script! ')


An object's __del__ is a way for an object to function when it is reclaimed by GC, and its execution generally means that the object cannot continue to reference


We don't call the __del__ method when we delete the property of the class, so we print it out before we go back and call the __del__ method,

The __del__ method will only go back to it when the memory is released.



But if we delete an instance of the F1 class, we will immediately release the memory, and Del will be called before print.

The __del__ method of Python's destructor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.