I. Built-in Method II
1.__del__ Method:
A: Constructs a method: Creates a space, the destructor method, releases a space;
B: The case that triggered del: the garbage collection mechanism of the Python interpreter, and the name of the Del object encountered
C: The idea of the destructor method:
classA:def __del__(self):Print(self) a=A ()delA#This first executes the __del__ method, then deletes object A, because the __del__ method is also passed to self;Print(a)#Output Result:<__main__. A object at 0x0000013cf5f59128> was executed.Traceback (most recent): File"d:/python/course/23. Module/DAY23/3-destructor method. py", line 49,inch<module>Print(a) Nameerror:name'a' is notDefined
D:python garbage collection mechanism:
class A: def __del__ (self): Print ('python interpreter will execute me after the program runs '= A ()print(a) # Output Result:<__main__. A object at 0x000001f823159128>python interpreter will execute me after the program runs
Summary: Del method: is to return/release some of the resources borrowed when creating the object (possibly the resources of the operating system)
Example: Some of the objects used in Python, like some common variables, have a Python interpreter to help you recycle,
and like some resources like the operating system borrowing need you to perform a recycling method to release yourself;
Like file handles, close operations, and some network resources;
3. Computer internal implementation of some small common sense:
A: Apply a space the operating system will assign to you B: everything within this space is managed by your Python interpreter;
So when you borrow the operating system resources, you have to go back before the end of the program, write the close code in the Del method;
python_021 (built-in method explained two)