Python's depth copy

Source: Internet
Author: User

#!/usr/bin/env python#-*-coding:utf-8-*-A='ABC'b=AA='def'Print(b)#This is the ABC printed here.#1. Execute a = ' abc ', the interpreter creates the string ' abc ' and variable A, and points a to ' ABC '#2. Execute B=a, the interpreter creates the variable B and points the variable B to the ABC that the variable a points to#3. Execute a = ' def ', the interpreter creates the string ' def ' and changes the direction of a to ' Def ', but B does not change:#so the final print is ABC.name={    "Z":{" Age": 20,"JX":"TN"},    "h":{" Age": 20,"JX":"TN"},    "x": 2,}name2=namename['Z'][' Age']=' A'Print('name is:', name)Print('name2 is:', name2)#Results#name is: {' Z ': {' JX ': ' tn ', ' age ': ' + '}, ' H ': {' JX ': ' tn ', ' Age ': $}}#name2 is: {' Z ': {' JX ': ' tn ', ' age ': ' + '}, ' H ': {' JX ': ' tn ', ' Age ' : ' +}}#here is the equivalent of making an alias, a soft connection, no longer occupy a piece of memory. This way, whether you change the name or the name2 is a piece of data for the operation. #With the first example there is a difference, for strings and numbers some data types of processing, will create a new copy of the data in memory, but the list, tuples, dictionaries, will not be processed like this. #Copy the first layername3=name.copy () name['x']='after the original dictionary has changed'name['h'][' Age']=' Age'Print(name)Print(Name3)#completely separate data, occupying memory of the same size as the meta dataImportCopyname4=copy.deepcopy (name)

Python's depth copy

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.