Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report