This is the 13th day of my participation in Gwen Challenge

The paper

In Python, object assignment and parameter passing are often used in order to understand their underlying principles. Python, like other languages such as Java, also introduced the concept of shallow text and deep copy.

First let’s be clear: variables are stored in stack memory and objects are stored in heap memory.

Python data types are mutable and immutable.

  • Mutable data types include lists, dictionaries, and collections
  • Immutable data types include: string, number, and tuple

  • Shallow copy:

    (1) Do not copy the content of the child object, only copy the reference of the child object

    (2) You can use the Python built-in function copy().

  • Deep copy:

    (1) All the memory of the child object will be copied, and the modification of the child object will not affect the source object

    (2) You can use the Python built-in function deepcopy().

The content outline is as follows:

1. The shallow copy

Shallow copy only copies references to source objects. For details, see the following section.

1.1 Principles of Single-Layer Shallow Copy
  • For source objects that are mutable data types, new space is created in heap memory

  • If the source object is an immutable data type, the reference is copied

Source objects are mutable data types

Print (id(a),id(c)) print(id(a),id(c)) print(id(a),id(c)Copy the code

The source object is an immutable data type

C = copy. Copy (a) print(id(a),id(c)) print(id(a),id(c)Copy the code

1.2 Mechanism of Nested shallow copy
  • For source objects that are mutable data types,

(1) Create new space in heap memory

(2) at the same address as the element in the source object

  • If the source object is an immutable data type, the reference is copied

Source objects are mutable data types

A = [(1, 2), (3, 4]] c = copy. The copy (a) print (id) (a), id (c)) # address different print (id (a [0]), id (c [0])) # nested tuple address is the same Print (id(a[1]),id(c[1])Copy the code

The source object is an immutable data type

A = ((1, 2), (3, 4]) c = copy. The copy (a) print (id) (a), print (c)) # same address print (id (a [0]), id (c [0])) # nested tuple address is the same Print (id(a[1]),id(c[1])Copy the code

2. Deep copy

2.1 Principle of Single-Layer Deep Copy

Single layer deep copy:

  • For source objects that are mutable data types, memory space is created in heap memory
  • For source objects that are immutable data types, copy the reference address

Source objects are mutable data types

Print (id(a),id(c)) print(id(a),id(c))Copy the code

The source object is an immutable data type

Deepcopy (a) print(id(a),id(c))Copy the code

2.3 Principles of Nested Deep Copy

Nested deep copy:

  • Is a mutable data type for source objects

(1) Create memory space in heap memory

(2) If the element in the source object is an immutable data type, then the reference address is copied directly

(3) Recursively create a new space if the element in the source object is a mutable data type

  • Is an immutable data type for the source object

(1) If the element in the source object is an immutable data type, then the reference address is copied directly

(2) Recursively create a new space if the element in the source object is a mutable data type

Source objects are mutable data types

A = [(1, 2), (3, 4]] c = copy. Deepcopy (a) print (id) (a), print (c)) # address different print (id (a [0]), id (c [0])) # nested tuple address is the same Print (id(a[1]),id(c[1])) print(id(a[1])Copy the code

A = ((1, 2), (3, 4)) c = copy. Deepcopy (a) print (id) (a), id (c)) # same address print (id (a [0]), id (c [0])) # nested tuple address is the same Print (id(a[1]),id(c[1])Copy the code

Assignment and the difference between shallow and deep copies

Assignment:

  • When an old variable is assigned to a new variable, the process actually happens in stack memory.
  • Both variables point to the storage space of the same object in stack memory.
  • When any variable changes, it points to a change in the address of the heap memory object, and the two variables are synchronized.
a = 1

c = a

Copy the code

Shallow copy:

  • Is a mutable data type for source objects

    (1) Recreate space in heap memory

    (2) Copy only the top-level memory space of the source object

    (3) the same address as the element in the source object

    (4) When the elements inside the new or source object change, they affect each other

  • If the source object is an immutable data type, the reference is copied

Deep copy:

  • For source objects that are mutable data types (1) space is recreated in heap memory

    (2) recursively copy its variable elements

    (3) For new or source object internal element changes, do not affect each other

  • If the source object is an immutable data type, the element data type is determined recursively, if it is mutable, a new address is created, and if it is invariant, the reference is copied

conclusion

Shallow copy and deep copy are actually copies of source objects.

If the source object has only one layer, any changes made to the source do not affect the depth copy object

If the source object has nested mutable data types, any changes made to the source will only affect the shallow copy, not the deep copy