Python third-party library series v-copy copy library (deep and shallow copy)

Source: Internet
Author: User
Tags shallow copy

recent work has not made any progress, I can only say ExtJS a little bit big, hard to chew ~


In Python, it is often possible to move from one object to another to facilitate subsequent computations, which can be implemented using copies.

Generally speaking, the copy is divided into three kinds: 1. pointer reference, 2. Shallow copy, 3. Deep copy.


One, pointer reference

Objects in Python are assigned by reference, and if you need to copy objects, you need to use the Copy module in the standard library.

Second, shallow copy

Copy.copy a shallow copy copies only the parent object and does not copy the inner child objects of the object.

Third, deep copy

Copy.deepcopy a deep copy of a copy object and its child objects.

Iv. practice (at a glance)

Import copy
a = [1, 2, 3, 4, [' A ', ' B ']]  #原始对象

b = A  #赋值, the reference to the object
C = copy.copy (a)  #对象拷贝, shallow copy
d = Copy.deepcopy (a)  #对象拷贝, deep copy

a.append (5)  #修改对象a
a[4].append (' C ')  #修改对象a中的 [' A ', ' B '] Array object

print ' A = ', a    #a = [1, 2, 3, 4, [' A ', ' B ', ' C '], 5]
print ' B = ', b    #b = [1, 2, 3, 4, [' A ', ' B '], ' C '], 5]
print ' c = ', c    #c = [1, 2, 3, 4, [' A ', ' B ', ' C ']]
print ' d = ', d    #d = [1, 2, 3, 4, [' A ', ' B ']]


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.