python_021 (built-in method explained two)

Source: Internet
Author: User

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)

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.