Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - A copy of a Python object.
A copy of a Python object.
Python assignment operation or function parameter passing always passes the object reference (i.e. memory address) instead of the object content. Everything is an object in Python, and objects can be divided into mutable and immutable types. Object replication refers to creating new objects in memory and generating new memory addresses. When the top-level object and its child objects are immutable immutable objects, there is no copy because no new objects are generated. Shallow copy, copy the top-level object, but do not copy the internal sub-objects. Deep copy, recursively copy the top-level object and its internal sub-element objects.

Everything in Python is an object, which is like a plastic box with data in it. There are different types of objects, such as Boolean and Integer, and the type determines what can be done with it. In real life, "pottery" will imply some information (for example, it may be heavy and fragile, so be careful not to fall to the ground).

The type of an object also determines whether the data it contains are modifiable variables (mutable) or unchangeable constants (immutable). You can imagine an immutable object as a transparent but closed box: you can see the data inside, but you can't change it. Similarly, a mutable object is like an open box. You can not only see the data inside, but also take it out and modify it, but you can't change the box itself, that is, you can't change the type of the object.

Object replication refers to creating new objects in memory and generating new memory addresses.

Shallow copy, copy the top-level object, but do not copy the internal sub-objects.

2. 1. 1. The top layer is mutable, and the child elements are immutable.

When the top-level object is mutable, but its child objects are immutable, such as [1,' world', 2]

① Create a list object and assign it to the variable a..

(2) Import the copy module, copy A lightly with the copy.copy () function, and assign it to the variable B..

③ Modify the sub-element a[0] of variable a[0] = 3. Because integers are immutable objects, instead of changing 1 to 3, A [0] is changed to point to 3.

When the top-level object is mutable, but the child elements also have mutable objects, such as [1, 2, ['hello',' world']]

(1) shallow copy.copy () only copies the top-level object, but does not copy the sub-element objects ['hello',' world'], that is, a[2] and b[2] point to the same list object.

② if a[2][ 1] =' China', then b[2][ 1] =' China'.

When the top-level object is immutable and all its sub-element objects are immutable, such as (1, 2,3).

Variables A and B point to the same tuple object and are not copied.

When the top-level object is immutable, but the child elements have mutable objects, such as (1, 2, ['hello',' world']).

Variables A and B point to the same tuple object, and a[2] and b[2] point to the same list, so modifying a[2][ 1] will affect b[2][ 1].

Deep copy, recursively copy the top-level object and its internal sub-element objects.

When the top-level object is mutable, but its child objects are immutable, such as [1,' world', 2]

Variable a and variable b point to different list objects. Modifying a[0] only points the first element of list A to the new object, and will not affect b[0].

When the top-level object is mutable, but the child elements also have mutable objects, such as [1, 2, ['hello',' world']]

Deep copy not only copies the top-level object, but also recursively copies the sub-element object, so a[2] and b[2] point to two different list objects (but the sub-elements of the list object initially specify the same string object). After modifying a [2][ 1] =' China', point to a new string object again (memory address is 1400).

When the top-level object is immutable and all its sub-element objects are immutable, such as (1, 2,3).

Variable a and variable b point to the same tuple object, and there is no copy.

When the top-level object is immutable, but the child elements have mutable objects, such as (1, 2, ['hello',' world']).

Variables A and B point to different tuple objects, and a[2] and b[2] point to different list objects, so modifying a[2][ 1] will not affect b[2][ 1].

Use = is assignment, that is, assign the reference of the list object to the variable B. You can imagine the list object as a box, and the variable A is equivalent to the label on the box. After executing b = a, it is equivalent to labeling the box with B, and A and B actually point to the same object. So whether we modify the contents of the list through A or B, the result will affect both parties.

B/c/d are all copies of A, all pointing to different list objects, but there are no copied child elements. A[2] and b[2]/c[2]/d[2] point to the same list, which is equivalent to the effect of shallow copy.

Using the slicing [:] operation, A and B actually point to the same tuple, and there are no copied sub-elements. a[2] and b[2] also point to the same linked list, which is equivalent to the effect of shallow copying.

Similar to lists, you can use the dictionary's copy () function or the conversion function dict ().

Variable a and variable b/c point to different dictionaries, but there are no copied sub-elements. A['jobs'] and b['jobs']/c['jobs'] specify the same list, which is equivalent to the effect of shallow copy.

Similar to lists, you can use the copy () function of set or the transformation function set ().

Variable a and variable b/c point to different sets, and the elements of the set must be hashed, so modifying set a will not affect b/c.