How the object life cycle is used in Python replication

Source: Internet
Author: User
This time to bring you how to use the object life cycle in Python replication, what are the considerations for replicating object lifecycles using Python, here's the actual case study, and take a look.

Shallow copy and deep copy of Python I don't know much, I don't know if copy1 and copy2 in Shutil are shallow copy and deep copy? These two operations, one is the file property rebuild, and the other is the file property is unchanged. Don't know if an object's replication can take this analogy?

Before copying, in order to be able to watch the GC's execution, define a special class and object. The test code is as follows:

Class Demo:    def del (self):       print ("removing{0}". Format (ID (self)) x = Demo () del X

The results of the program execution are as follows:

e:\01_workspace\02_programme_language\03_python\03_oop\2017\08>pythoncopy1.py
Removing1772226785520

The following example constructs a shallow copy:

>>>obj_list1 = [Demo (), demo ()]>>>obj_list2 = obj_list1[:]>>> del obj_list1>>> del obj_list2removing1902510644416removing1902510644360

From the above results, the first del does not trigger the object destruction method execution. This is the result of debugging in the interactive interface, and if the code is written to a specific file, the results of the program execution may be different. When all statements are written to the same file, the object scope in the file is slightly adjusted. This is worthy of our attention.

Another common method of shallow replication:

>>> O1 =o2 = Demo () >>> del o1>>> del o2removing1902510136568

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

Python how to read and write TXT file line-wise

Python how to bulk read TXT file to dataframe format

Related Article

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.