Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand python pointer copy, shallow copy and deep copy

Shulou Source: shulou.com Published: 2022-06-02 18:26:15 09月12日 Update

This article mainly explains "how to understand python pointer copy, shallow copy and deep copy". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand python pointer copy, shallow copy and deep copy.

First of all, for the immutable type int,string,float,tuple, you don't have to worry about copying. It can be considered that each copy will recreate the memory space to store it, and modifying the new value will not affect the original one; the two are not related except for the value.

For variable type Dict set list, as well as instance objects and classes, pointer copy, shallow copy and deep copy should be considered when copying.

-pointer copy: usually A = B, for variable types, this copy is an extra pointer, two Amemery B points to the same memory space, and any modification will affect the other.

Class TreeNode: def _ _ init__ (self, x): self.val = x self.left = None def _ str__ (self): return "[{}: {}]" .format (self.val, self.left) NodeA = TreeNode ('A') NodeB = TreeNode ('B') NodeA.left = NodeBprint (NodeA) NodeC = NodeAprint (NodeC) print ('pointer copy Update NodeC and update the original NodeA') NodeC.val = 'C'NodeC.left = Noneprint (NodeA) print (NodeC)

The result is:

[a: [B: None]] [A: [B: None]] pointer copy, update NodeC, and update the original NodeA [C:None] [C:None]

-shallow copy: the implementation of the import copy library is required. Here, a new memory space is used to store the copied content. However, if the copy points to another mutable type, the reference still points to the original memory space of that mutable type.

Import copyNodeA = TreeNode ('A') NodeB = TreeNode ('B') NodeA.left = NodeBprint (NodeA) NodeC = copy.copy (NodeA) print (NodeC) print ('shallow copy, update NodeC, will not update the original NodeA, but update reference NodeC.left will affect NodeA.left') NodeC.val =' C'NodeC.left.val = 'Cleft'print (NodeA) print (NodeC)

Results:

[a: [B: None]] [A: [B: None]] shallow copy, update NodeC, will not update the original NodeA, but update reference NodeC.left will affect Node A.left [A: [left: None]] [C: [left: None]]

-Deep copy: the implementation of the import copy library is required. Here, a new memory space is used to store the copied content; if there is a pointer to the variable type space, it will also be copied.

Import copyNodeA = TreeNode ('A') NodeB = TreeNode ('B') NodeA.left = NodeBprint (NodeA) NodeC = copy.deepcopy (NodeA) print (NodeC) print ('deep copy, update NodeC, will not update original NodeA, update NodeC.left, will not affect NodeA.left') NodeC.val =' C'NodeC.left.val = 'Cleft'print (NodeA) print (NodeC)

Results:

[a: [B: None]] [A: [B: None]] deeply copy, update NodeC, will not update the original NodeA, update NodeC.left, will not affect NodeA.left [A: [B: None]] [C: [left: None]] so far, I believe you have a deeper understanding of "how to understand python pointer copy, shallow copy and deep copy". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Copy update pointer space type variable influence memory content point result two at the same time learning practical deeper no interest instance Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Xiaomi Shulou Technology Redmi Huawei