Deep copy and shallow copy in Python

Source: Internet
Author: User
Tags shallow copy in python

This article mainly introduces the deep copy and the shallow copy detailed explanation in Python, this article explained the variable-object-reference, the Changeable object-immutable object, the copy and so on content, needs the friend to be possible to refer to under

To make sense of the deep and shallow copies of Python, you need to understand the following series of concepts:

Variables-References-Objects (Mutable objects, immutable objects)-slices-copies (shallow copies, deep copies)

"Variable-Object-Reference"

Everything in Python is an object, say: 3, 3.14, ' Hello ', [1,2,3,4],{' a ': 1} ...

Even the type itself is an object, the type Object

In Python, a variable differs from C/c++/java in that it refers to an object's reference, Python is a dynamic type, and the program runs according to the type of object

To determine exactly what type of variable.

Assign values individually: for example,

Copy code code 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's memory space

Because the python variable is simply a reference to an object, or a pointer to an object, you can often change a variable reference in a program

Copy code code as follows:

>>> x = #变量绑定到整型对象

>>> x = ' Hello ' #现在又成了字符串

>>> x = [1,2,3] #现在又成了列表

The professional statement is as follows:

A variable is an element of a system table that has space to connect to an object

An object is an allocated piece of memory that stores the value it represents

A reference is a pointer to an object that is automatically formed from a variable

Special Note: Type belongs to object, not variable

Like the A=3, the integer object 3 contains the dual message.

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, say:

Copy code code as follows:

>>> A = 3

>>> B = A

After running assignment statement B = A, variable A and variable B point to the memory space of the same object.

As you can see from the image above, A and B have exactly the same ID, pointing to the same integer object 3, or the same memory.

If you delete a, it will not affect B

The introduction of the concept of copying 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 they are created, including strings, meta ancestors, numbers

mutable objects in Python refer to: Objects that can be modified, including: Lists, dictionaries

The a,b are all integers, integers are immutable objects, and if they are mutable objects, that's another thing.

Copy code code 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] = #因为列表可变, changing the value of the first element in L1

>>> L1; L2 #改变后, l1,l2 at the same time because the value of the object itself has changed

[200, 3, 4]

[200, 3, 4]

If you don't want to change the value of the list L2, there are two ways: slices and copy modules

Copy code code as follows:

>>> L1 = [2,3,4]

>>> L2 = L1

>>> ID (L1); ID (L2) #共享引用一个可变对象

45811784L

45811784L

>>> L2 = l1[:] #切片操作

>>> ID (L1); ID (L2) #切片后, objects are different.

45811784L

45806920L

>>> L1[0] = 200

>>> L1; L2 #L1发生改变, L2 no change.

[200, 3, 4]

[2, 3, 4]

Copy

1. Slicing technology applies to all sequences, including: Lists, strings, 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 the sequence, but also for the dictionary

Copy code code as follows:

>>> Import Copy

>>> X = copy.copy (Y) #浅拷贝: Copy only top-level objects, or: parent objects

>>> X = copy.deepcopy (Y) #深拷贝: Copy 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 you have nested objects in the dictionary:

Conclusion

Deep copies are copies of the source object, taking up different memory space

If the source object has only a level of directory, the source makes any changes, does not affect the shade copy object

If the source object has more than one directory, the source makes any changes to affect the shallow copy, but does not affect the deep copy

A slice of a sequence object is actually a shallow copy, that is, only the top-level object is copied

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.