In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the citation and copying law in Python". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the citation and copying law in Python".
Python quotes everything.
In C++/Java, int a = 1 is to create the variable a, and to assign it to 1 position int b = an is to create the variable b, which is assigned to the value of a. An and b are irrelevant, that is, "variables are boxes," but this is not good for understanding a variable definition in Python. In Python, we think of variables as "an actual stored reference" (graphic source: "smooth python").
So in python, a = [1Magne2Magol 3] allocates an area to write [1Magne2Magne3], and then lets a represent it; b = a makes b and a represent the same thing, and even if an itself disappears (such as del a), it is just tearing off a tag, and b can still access the list. The same is true of other types.
Case 1: direct reference
The direct reference is b = a, and as mentioned above, there is no copy, just let b also represent the area that a represents. At this time, b is a memory b [0], that is, a [0].
If you modify a, you make a point to another object, which has nothing to do with the list, so b does not change; if you modify a [0] (or use + =, append, etc.), you modify the list, and b [0] is also changing.
But for immutable objects such as a single number or tuple string, you can also use + =, but they don't support in-situ modification, so they actually call a = a + b to get a new object. If a = (1,2,3); b = a; a + = (4,5), execute a = a + (4,5) at this time, it already points to the new value, so b will not change.
Case 2: replication
Sometimes we only edit copies of lists or dictionaries, so we need to copy them. Generally speaking, the most common replication methods are:
B = a [:] b = list (_ for _ in a) b = copy (a) b = a.copy ()
These are called shallow copies. What happens when shallow copies are made?
The logic of shallow copying creates a new object, and then a copy of each value is put into the new object, taking linear time. You can see that the copied b is exactly the same as a, but an is b is no longer valid, and a [0] and b [0] are also irrelevant values. You can modify the list b at will, without affecting the four elements in a (red, blue, orange and green circles).
Case 3: deep replication
But shallow replication still has some problems that can't be solved. We know that everything is quoted in python, and the little circle in the picture is not a box but a label! Although an and b themselves are separated, if an element is still a list, they are actually related.
As shown in the figure, b [1] = a [1] is performed during shallow copy, but b [1] and a [1] are references, so the same variable sequence is still accessed through them, and modifying a [1] does not result in b [1] change. but modifying a [1] [0] leads to a change in b [1] [0].
So we introduce deep replication to solve this problem:
From copy import deepcopya = [1, [1,2,3], "hello"] b = deepcopy (a)
The logic of deep replication is to copy each value into a new object, and if this item also represents a mutable iterative object (list, dictionary, no custom class), make a copy of that object as well. In this way, you can get a complete copy.
Thank you for your reading, the above is the content of "what is the citation and copying law in Python". After the study of this article, I believe you have a deeper understanding of what the citation and copying law in Python is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.