Deep copy and shallow copy in **python

Source: Internet
Author: User

Deep copy and shallow copy in Python

This article mainly introduces the deep copy and shallow copy in Python, this article explains the variables-object-Reference, Mutable object-immutable object, copy and so on. To get a clear copy of Python, you need to understand the following concepts: variables-References-Objects (Mutable objects, immutable objects)-slices-copy (shallow copy, deep copy) "Variable-object-Reference"In Python everything is an object, such as: 3, 3.14, ' Hello ', [1,2,3,4],{' a ': 1} ...

Even the type itself is an object, type Object

The variables in Python are different from C/c++/java, which refers to the object's reference, Python is a dynamic type, and when the program runs, it is based on the type of the object
To determine what type of variable it is.

Assign a value individually: say, for example:

The code is as follows:
>>> A = 3
After running a=3, variable a becomes a reference to object 3. Internally, the variable is actually a pointer to the object memory space

Because a python variable is simply a reference to an object, or a pointer to an object, variable references can be changed frequently in a program

The code is as follows:
>>> x = #变量绑定到整型对象
>>> x = ' Hello ' #现在又成了字符串
>>> x = [#现在又成了列表]
The professional statement is as follows:

A variable is an element of a system table that has a connection to an object
object is a piece of memory that is allocated, storing the value it represents
A reference is an automatically formed pointer from a variable to an object
Special note: Types belong to objects, not variables


For example, like the A=3, the integer object 3 contains the dual information
1. Value is 3
2. A header message: Tell Pthyon that this is an integer object [equivalent to a pointer to int]

Shared references: for example,

The code is as follows:
>>> A = 3
>>> B = A
After you run assignment statement B = A, the variable A and variable B point to the memory space of the same object.

As you can see, A and B, whose IDs are exactly the same, point to the same integer object 3, or the same piece of memory.

If you delete a, it will not affect B

The introduction of copy concepts is based on the potential side effects of shared references to mutable objects.


"Mutable objects-immutable objects"
Immutable objects in Python refer to objects that cannot be modified once created, including strings, tuples, numbers
mutable objects in Python refer to: Objects that can be modified, including: Lists, dictionaries
The above said a, b are integers, integers are immutable objects, if they are mutable objects, it is another matter.

The code is as follows:
>>> L1 = [2,3,4] #L1变量指向的是一个可变对象: List
>>> L2 = L1 #将L1值赋给L2后, both share references to the same list object [1,2,3,4]
>>> l1[0] = #因为列表可变, change the value of the first element in the L1
>>> L1; L2 #改变后, l1,l2 change at the same time, because the value of the object itself has changed.
[200, 3, 4]
[200, 3, 4]
If you do not want to change the value of the list L2, there are two ways: Slice and copy moduleCopy CodeThe code is as follows:
>>> L1 = [2,3,4]
>>> L2 = L1
>>> ID (L1); ID (L2) #共享引用一个可变对象
45811784L
45811784L
>>> L2 = l1[:] #切片操作
>>> ID (L1); ID (L2) #切片后, the object is different.
45811784L
45806920L
>>> L1[0] = 200
>>> L1; L2 #L1发生改变, L2 no change
[200, 3, 4]
[2, 3, 4]

Copy
1. Slicing techniques are applied to all sequences, including: List, String, Ganso
>>> but slices cannot be applied to dictionaries. You can only use the D.copy () method or the D.deepcopy () method for a dictionary.

2. Deep copy, can be used for sequence, also can be used in dictionary

The code is as follows:
>>> Import Copy
>>> X = copy.copy (Y) #浅拷贝: Copies only top-level objects, or: parent objects
>>> X = copy.deepcopy (Y) #深拷贝: Copies all objects, top-level objects, and their nested objects. Or, the parent object and its child objects
If the dictionary has only top-level objects:

If the object is nested in the dictionary:

Conclusion

A deep copy is a copy of the source object, taking up different memory space
If the source object has only one level directory, the source makes any changes, does not affect the dark Copy object
If the source object has more than one level of directory, the source makes any changes to affect the shallow copy, but does not affect the deep copy sequence object's slice is actually the shallow copy, namely only the top-level object

Deep copy and shallow copy in **python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.