The official start of the
First of all, I’m going to introduce two new little things here that I’m going to use next. One is the function id() and the other is the operator is. The id() function returns the memory address of the object; Is is to compare whether the object references of two variables point to the same object. Do not confuse it with == here. == is to compare whether the values of two variables are equal.
> > > a = [1, 2, 3] > > > b = [1, 2, 3] > > > id 38884552 l (a) > > > a is b False > > > a = = b TrueCopy the code
Copy this word has two ways to call, one is according to its pronunciation transliteration, called copy; The other is the standard translation, called copying.
To copy is to make a second copy of something, but in many programming languages, such as Python and C++, it’s not as simple as that.
>>> a = 1
>>> b = a
>>> b
1
Copy the code
Looking at the example above, it might seem like we have two ones on the surface, but if you’ve read my previous post, you’ve got the phrase “variables have no type.” A variable in Python is a tag. Here we have id() to pop in and see where they are in memory.
>>> a = 1
>>> b = a
>>> b
1
>>> id(a)
31096808L
>>> id(b)
31096808L
Copy the code
Do you see, id(a) and ID (b) are the same, so there are no two 1’s, just one 1, just two labels attached to the 1, named a and b. This phenomenon is common in Python, and this assignment implements “pretend” copying. The real situation is a reference between two variables and the same object.
Let’s look at the copy() method:
>>> a = {'name':'rocky'.'like':'python'}
>>> b = a.copy()
>>> b
{'name': 'rocky'.'like': 'python'}
>>> id(a)
31036280L
>>> id(b)
38786728L
Copy the code
Well, sure enough, this time I get a different B than THE original A, which is another space in memory. So what we’re doing here is we’re saying, well, they’re the same, but they’re in two different memory Spaces, so they’re not interfering with each other, and if we change B, then A is not interfering with each other.
>>> b['name'] = 'leey'
>>> b
{'name': 'leey'.'like': 'python'}
>>> a
{'name': 'rocky'.'like': 'python'}
Copy the code
The result is exactly the same as we reasoned above, so by understanding that objects have types, variables have no types, and variables are labels to objects, you can correctly infer the results provided by Python.
We are going to look at an example, please make sure you understand the above when reading, otherwise it is easy to get carsick.
>>> a = {'name':'rocky'.'like':'python'}
>>> b = a
>>> b
{'name': 'rocky'.'like': 'python'}
>>> b['name'] = 'leey'
>>> b
{'name': 'leey'.'like': 'python'}
>>> a
{'name': 'leey'.'like': 'python'}
Copy the code
Do you see anything in the example above? The dictionary type of object for B is changed. The object for A is also changed. In other words, b = a results in two variables referring to the same object, but is it really that simple? Keep your eyes open and look down. This is the point.
>>> first = {'name':'rocky'.'lanaguage': ['python'.'c++'.'java']}
>>> second = first.copy()
>>> second
{'name': 'rocky'.'lanaguage': ['python'.'c++'.'java']}
>>> id(first)
31036280L
>>> id(second)
38786728L
Copy the code
That’s not a problem here, as we said before, second is copied from first, and they refer to two objects.
>>> second['lanaguage'].remove('java')
>>> second
{'name': 'rocky'.'lanaguage': ['python'.'c++']}
>>> first
{'name': 'rocky'.'lanaguage': ['python'.'c++']}
Copy the code
Did you find anything? In the above example, the lanaguage of second corresponds to a list. If I delete the value in the list, only second should be changed. Why should I change the lanaguage of first? Was it a surprise? Is that what we were saying wrong? So let’s try another key:
>>> second['name'] = 'leey'
>>> second
{'name': 'leey'.'lanaguage': ['python'.'c++']}
>>> first
{'name': 'rocky'.'lanaguage': ['python'.'c++']}
Copy the code
The principle above is valid, so why is it? Come on, let’s welcome id() again.
>>> id(first['name'])
38829152L
>>> id(second['name'])
38817544L
>>> id(first['lanaguage'])
38754120L
>>> id(second['lanaguage'])
38754120L
Copy the code
The underlying reason for this is the way Python stores data, which I won’t go into too much detail about. The only thing we need to know here is that when copy() is used, lists, which are composed of strings, numbers, etc., are still copied references, that is, labeled, without creating a new object. This is called a shallow copy. There are ways to solve the deeper problems.
Indeed, there is a deep copy in Python, which requires a copy module to be introduced before it can be used. Let’s give it a try.
>>> import copy
>>> first = {'name':'rocky'.'lanaguage': ['python'.'c++'.'java']}
>>> second = copy.deepcopy(first)
>>> second
{'name': 'rocky'.'lanaguage': ['python'.'c++'.'java']}
>>> second['lanaguage'].remove('java')
>>> second
{'name': 'rocky'.'lanaguage': ['python'.'c++']}
>>> first
{'name': 'rocky'.'lanaguage': ['python'.'c++'.'java']}
Copy the code
When you use a deep copy, it’s not a quote.
Write in the last
For more content, please follow the public account “Python Space”, looking forward to communicating with you.