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