#!/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