Python shortest and deep copy usage instances and python usage instances
This example describes the usage of Python shortest copy and deep copy. Share it with you for your reference. The specific analysis is as follows:
>>> Person = ['name', ['savings ', 100] >>> hubby = person [:] >>> wifey = list (person) >>> [id (x) for x in person, hubby, wifey] [3074051788L, 3074061740L, 3074061996L] >>> [id (y) for x in person, hubby, wifey for y in x] [3074319552L, 3074259916l, 3074319552L, 3071099916l, 3074319552L, 3074259916l] >>> hubby [0] = 'job' >>> wifey [0] = 'jar' (['job', ['savings ', 100], ['jar', ['savings ', 100], ['name', ['savings', 100])> hubby [1] [1] = 70 >>> hubby, wifey, person (['Joe ', ['savings', 70], ['Jane ', ['savings ', 70], ['name', ['savings', 70])
A small copy creates a new object, but its object reference is indeed consistent with that of the Copied object.
If the elements of the object to be copied are of an immutable type, the changes made to this element by the Copied object are actually creating a new object.
If the elements of the object to be copied are of a variable type, the changes made to this element by the Copied object are actually modified in the memory space of the original object.
In this way, if hubby [1] [1] = 70, the corresponding value of wifey and person will change.
I hope this article will help you with Python programming.