current position:Home>Python series tutorial 127 -- Reference vs copy

Python series tutorial 127 -- Reference vs copy

2022-01-31 23:38:23 AI technology of artificial intelligence

friends , If you need to reprint, please indicate the source :blog.csdn.net/jiangjunsho…

Statement : During the teaching of artificial intelligence technology , Many students mentioned to me python Related issues , So in order to let the students master more extended knowledge and better understand AI technology , I put the assistant in charge of sharing this python Series of tutorials , I hope that helps ! Because of this python The tutorial wasn't written by me , So it's not as good as mine AI Technology teaching is humorous , It's boring to learn ; But its knowledge is in place , It's also worth reading !PS: If you don't understand this article, please read the previous article first , Step by step, learn a little everyday, and you won't find it difficult !

We have mentioned , An assignment is a reference to a stored object , Not copies of these objects . Because the assignment operation will produce multiple references to an object , Modifying an object may affect other references to the object elsewhere in the program . If you don't want to change it all , Then you need to tell Python Copy the object , Instead of simply assigning values .

for example , The following example creates a list X, And another list L,L Nested pair list X References to . In addition, a dictionary has been created D, Contains another pair list X References to .

>>> X = [1,2,3]

>>> L = ['a',X,'b']  # Embed references to X's object

>>> D = {'x':X,'y':2}
 Copy code 

In the code above , There are three references to the same object list :X、L The second element of and D The first element of .

Because of the variable X The list of references is also being L and D Quote inside , Because the list is variable , Modify any of the three references to the list object , It will also change the other two referenced objects at the same time .

>>> X[1] = 'surprise'  # Changes all three references!

>>> L

['a',[1,'surprise',3],'b']

>>> D

{'x': [1,'surprise',3],'y': 2}
 Copy code 

Most of the time , This feature of reference is beneficial to us , Because you can pass large objects anywhere within the program without having to make copies on the way , thus , It can save memory and improve running speed . However , If you don't want this one change all feature , Then you need to explicitly copy .

• Slicing expressions without constraints (L[:]) Able to copy sequences . • Dictionaries copy Method (X.copy()) Be able to copy dictionaries . • Some built-in functions ( for example ,list) Can also generate copies (list(L)).

for instance , If you have a list and a dictionary , You don't want to change their values through other variable references .

>>> L = [1,2,3]

>>> D = {'a':1,'b':2}
 Copy code 

You can assign copies to other variables , Instead of copying by reference .

>>> A = L[:]     # Instead of A = L (or list(L))

>>> B = D.copy()  # Instead of B = D (ditto for sets)
 Copy code 

thus , Changes made by other variables only modify the copy , Not the original object .

>>> A[1] = 'Ni'

>>> B['c'] = 'spam'

>>>

>>> L,D

([1,2,3],{'a': 1,'b': 2})

>>> A,B

([1,'Ni',3],{'a': 1,'c': 'spam','b': 2})
 Copy code 

For the example at the beginning of the article , You can avoid references by slicing the original list .

>>> X = [1,2,3]

>>> L = ['a',X[:],'b']   # Embed copies of X's object

>>> D = {'x':X[:],'y':2}
 Copy code 

L and D Now it will point to a different list instead of X. thus , adopt X The changes made can only affect X Without affecting L and D. Similarly , modify L or D It won't affect X.

It should be noted that : Partition of unconditional values and Dictionary copy Method can only do top-level replication . in other words , Nested data structures cannot be copied . If you need to make a complete copy of a deeply nested data structure , Then use the standard copy modular —— First import copy, And then execute X = copy.deepcopy(Y) To any nested object Y Make a complete copy . This call statement can recursively traverse objects to copy all their components .

copyright notice
author[AI technology of artificial intelligence],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201312338220230.html

Random recommended