Shallow copy and deep copy in Python

Source: Internet
Author: User

A shallow copy can take a husband and daughter-in-law a bank card for example  
# python>>> husband = [' Liang ', 123,[10000,6000]] #丈夫的银行卡信息 (name, ID, quota, balance) >>> wife = Husband.copy ( #媳妇和丈夫公用一张银行卡 (the name differs from the ID, but the amount is the same as the balance) >>> wife[0] = ' Yue ' >>> wife[1] = 456>>> wife[2][1]-= #媳妇花费3000 >>> wife[' Yue ', 456, [10000, +]] #显示媳妇余额 >>> husband[' Liang ', 123, [10000, []] #显示丈夫余额

  

A shallow copy is just a copy of the first layer, a deep copy equivalent to a clone and a deep copy as follows:

Introduced small three, small three spent money daughter-in-law don't know:>>> liang = [' xin ',111,[15000,10000]]>>> yue = liang.copy () >>> yue[' Xin ' , 111, [15000, 10000]]>>> yue[0] = ' Mumu ' >>> yue[1] = 222>>> yue[2][1]-= 5000>>> yue[ ' Mumu ', 222, [15000, 5000]]>>> liang[' Xin ', 111, [15000, 5000]] above is a shallow copy; The following is a deep copy:>>> import copy>>& Gt Xiaosan = Copy.deepcopy (Liang) >>> xiaosan[0] = "* *" >>> xiaosan[1] = 333>>> xiaosan[2][1]-= 1999>>> xiaosan[' * * ', 333, [15000, 3001]]            #已经减少啦 >>> liang[' Xin ', 111, [15000, 5000]]>>> yue[' Mumu ', 222, [15000, []]       #媳妇不知道

  

Shallow copy and deep copy in Python

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.