Python: Copy copy (deep copy deepcopy and shallow copy copy)

Source: Internet
Author: User
Tags shallow copy

When assigning values between objects in Python is passed by reference, the copy module in the standard library is required if the object needs to be copied.

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

2. Copy.deepcopy deep copy Copy object and its sub-object

>>>Import Copy>>> a = [1,2,3,4,['a','b']] #原始对象>>> B =a #赋值, a reference to a passing object>>> C =Copy.copy (a)>>> d =Copy.deepcopy (a)>>> A.append (5)>>> a[4].append ('C')>>> Print'a=', AA= [1,2,3,4, ['a','b','C'],5]>>> Print'b=', BB= [1,2,3,4, ['a','b','C'],5]>>> Print'c=', CC= [1,2,3,4, ['a','b','C']]>>> Print'd=', DD= [1,2,3,4, ['a','b']]

Original link: http://www.cnblogs.com/Richardzhu/p/4723750.html

Python: Copy copy (deep copy deepcopy and shallow copy 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.