Python shallow copy and deep copy

Source: Internet
Author: User
Tags shallow copy

Python variables are stored in memory like this:

In Python, everything is an object, and the object's storage is a reference store, storing only the memory address where the value of the variable resides, not the value of the variable itself.



If you modify the value, you are actually creating a new value in memory that points to the address of the value



You can see that the address has changed.

If two values are equal, then changing one does not affect the other


A=b A, B points to the same address

b does not change after changing the value of a


The description is a new value created in memory and a points to the address of this value



This is the basic type of storage

Python can be divided into two categories of data types, one is the underlying data type, such as the int str float,long, BOOL and other basic types, the other is list,tulpe,dict,set and other complex types, list,tulpe,dict, The set contains the underlying type

Since the variables in Python are all reference stores, and list,tuple,dict this data structure can contain the underlying data type, this causes the address of the variable to be stored in each variable, not the value itself; For complex data structures, The storage inside is just the address of each element.

If the list is stored in the form:

L = [1,2,3,' a ',' B ', ' C ']

Print (ID(L))

The address of the variable L is



The address of each element is

L = [1,2,3,' a ',' B ', ' C ']

#print (ID (L))

Print (ID(l[0]))

Print (ID(l[1]))

Print (ID(l[2]))

Print (ID(l[3]))

Print (ID(l[4]))



The address of these elements is what l have stored.

In memory representations are:

Assign the contents of the list L to another variable and character operation

L = [1,2,3,' a ',' B ', ' C ']

Y = L

Print L

Print (Y)

Print (ID(L))

Print (ID(Y))




Append element to L:

L = [1,2,3,' a ',' B ', ' C ']

Y = L

Print L

Print (Y)

Print (ID(L))

Print (ID(Y))

L.append ("F")

Print L

Print (Y)

Print (ID(L))

Print (ID(Y))



If re-assigned or reinitialized, the new memory space is reassigned

L = [1,2,3,' a ',' B ', ' C ']

Y = L

Print L

Print (Y)

Print (ID(L))

Print (ID(Y))

L.append ("F")

Print L

Print (Y)

Print (ID(L))

Print (ID(Y))

L = [' A ',' B ',' C ']

Print L

Print (Y)

Print (ID(L))

Print (ID(Y))



That is, if you operate on an address then the contents of the variable pointing to the two address will change but the address points to the same

If the value is re-assigned, the re-assigned variable address will point to the new memory address

This is the process of assigning a variable value.

Shallow copy

Shallow copy: No matter how complex the data structure, shallow copy will only copy one layer.

A copy of the underlying data, whether a shallow copy or a deep copy, will reassign the memory address

#基础数据拷贝

Import Copy

L = [1,2,3,' a ',' B ' ]

M = Copy.copy (L)

Print (L)

Print (M)

Print (ID(L))

Print (ID(M))




Import Copy

L = [1,2,3,' a ',' B ']

M = Copy.copy (L)

Print L

Print M

Print (ID(L))

Print (ID(M))

#给L追加一个元素M不会变

L.append ("C")

Print L

Print M

Print (ID(L))

Print (ID(M))




Deep copy of basic data:

Import Copy

L = [1,2,3,' a ',' B ']

M = Copy.deepcopy (L)

Print L

Print M

Print (ID(L))

Print (ID(M))

#给L追加一个元素M不会变

L.append ("C")

Print L

Print M

Print (ID(L))

Print (ID(M))



The result is the same as a shallow copy

Complex data:

L Change m unchanged only after modifying the copy of the external layer

Import Copy

L = [1,2,[3,4,' C ', ' d '] , ' A ' , ' B ']

M = Copy.copy (L)

Print L

Print M

Print (ID(L))

Print (ID(M))

#给L追加一个元素M不会变

L.append ("C")

Print L

Print M

Print (ID(L))

Print (ID(M))




Changes in the inner layer, then the copy will change

Import Copy

L = [1,2,[3,4,' C ', ' d '] , ' A ' , ' B ']

M = Copy.copy (L)

Print L

Print M

Print (ID(L))

Print (ID(M))

l[2].append ("E")

Print L

Print M

Print (ID(L))

Print (ID(M))

This means that the modification of the inner layer data will change the object of the other copy.

In memory, the form is:


As you can see, the inner layer list[1,2] is also an address, which is the address of the element

So the inner operation of L is the operation of the inner layer list[1,2] address, which will change M

Deep copy:

Import Copy

L = [1,2,[3,4,' C ', ' d '] , ' A ' , ' B ']

M = Copy.deepcopy (L)

Print L

Print M

Print (ID(L))

Print (ID(M))

#给L追加一个元素M不会变

l[2].append ("E")

Print L

Print M

Print (ID(L))

Print (ID(M))


Can see L change m unchanged

In-memory form:


Equivalent to all copies of a copy.

1, for complex data structure copy.copy shallow copy copies only the parent object (the outer layer), the object's inner (inner data) sub-object is not copied.

2. copy.deepcopy deep Copy copy of parent object (outer data) and its sub-objects (inner data)

3, for the basic data type deep copy of shallow copy all the same, will generate new data space

Python shallow copy and deep copy

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.