Python object assignment, a shallow copy and deep copy


Python, the basic data types, understood as a common data types: Boolean, integer, floating point, strings, lists, tuples, dictionaries, collections, with different languages ​​and different, but according to storage in memory different , distinguish atom type and the container type.

Object assignment

Assigning objects is performed (pass object references) / (memory address transfer) / (memory reference), so that when an object is changed, the other sync change.

Consideration binding Code
?????
Will = [ "Will", 28, [ "the Python", "C #", "the JavaScript"]] # Data type of atomic element types and container types
Wilber = Will
Print ID (Will) Will # the id
Print Will
Print [id (ELE) for ELE in Will] id # in each element Will
print id (wilber) # wilber of the id == will id
Print Wilber

will [0] = "Wilber" # Will change in the element 0
Will [2] .append ( "the CSS") # Will change in the element 2
Print id (Will) Will the id # unchanged
Print Will
Print [id ( ELE) for ELE in by Will]
Print the above mentioned id (Wilber) # Wilber
Print Wilber
Print [the above mentioned id (ELE) for ELE in Wilber]
· · ·

Shallow copy

Shallow copy object is a container type, only the object type assignment atoms. Shallow copy will serve as a new target, the memory address of the original container contains a reference to the past. When elements contained in the original container has variable (i.e. a container object), to change the container element can also cause changes to the new copy of the object element.

Thinking binding Code

import copy

will = ["Will", 28, ["Python", "C#", "JavaScript"]]
wilber = copy.copy(will)

print id(will)
print will
print [id(ele) for ele in will]
print id(wilber)
print wilber
print [id(ele) for ele in wilber]

will[0] = "Wilber"
will[2].append("CSS")
print id(will)
print will
print [id(ele) for ele in will]
print id(wilber)
print wilber
print [id(ele) for ele in wilber]

Deep copy

Deep copy of the object is a container type, only the object type assignment atoms. Deep copy and shallow copy, as will be based on a deep copy of the original object from a new object, but a new type of container will copy the elements inside a (new default copy layers of depth seems to be 1), so the container element of the original object changed, container element of the new object will not change.

Thinking binding Code

import copy

will = ["Will", 28, ["Python", "C#", "JavaScript"]]
wilber = copy.deepcopy(will)

print id(will)
print will
print [id(ele) for ele in will]
print id(wilber)
print wilber
print [id(ele) for ele in wilber]

will[0] = "Wilber"
will[2].append("CSS")
print id(will)
print will
print [id(ele) for ele in will]
print id(wilber)
print wilber
print [id(ele) for ele in wilber]

to sum up

Data structures + algorithms, understand the need to repeatedly learn + + memory.


https://www.cnblogs.com/wilber2013/p/4645353.html#_nav_1

Guess you like

Origin www.cnblogs.com/bqwzx/p/10983952.html