Assignment, shallow copy, deep copy in Python

Source: Internet
Author: User
Tags shallow copy

Before understanding shallow and deep copies, first understand how variables are stored in Python.

The type of the variable is the two types of the score reference and the address reference.

All variables in Python are objects, stored in variables, using address references, storing only the memory address where the value of a variable resides, not the variable itself.

In Python, there are multiple data types: bool, int, long, float, string, list, dict, tuple, set;

which can be divided into basic data types and complex data structures;

Basic data types: bool, int, long, float, string;

Complex data structures: list, dict, tuple, set; (For variables that can store a basic data type can be seen as complex data structures)

Because variable storage in Python is an address reference, variables stored in complex data structures (list, dict, tuple, set) are simply addresses of variables.

First, the initialization of the variable (assigned value):

The initialization of a variable in 1.Python is a new memory in memory that stores the value of the variable, assigning the address of the memory to the variable. (The address where the value is stored in the variable)

1 ' ABC ' 2 >>> ID (str_1)  3 4300773168 4 >>> str_2 = str_1  5 >>> ID (str_2)  6 4300773168

When you reinitialize a variable that has already been initialized, the address in the variable is changed again.

1 >>> str_1 = 1232 >>> ID (str_2)3 43007731684 >> > ID (str_1)5 4297541792

2. In complex data structures, changing the elements (basic data types) in a data structure simply alters the address of the element without affecting the address of the complex structure itself.

1 >>> list_1 =[1,2,3]2 >>> ID (list_1)34320183368  4list_1.append (9)5list_1[2] =6List_1.pop ( 2)7 38print(List_1,id (list_1))9  4320183368

Make a conclusion through the above example:

When complex data structures (list, dict, tuple, set) are assigned, a piece of memory is created in the computer to store the value of the complex data structure, while modifying the elements of the element only changes the address of the complex data structure, without affecting the address of the complex data structure ( It's like sitting on a table for two people, and two people on the table is shared ); When re-assigning, the new memory is re-opened to store the value of the variable, and the address of the memory is stored in the variable. (the equivalent of a person on the table was moved to another table to eat)

Second, copy:

In Python, we sometimes save a piece of data and then process it, and this time Python offers two copies: a shallow copy and a deep copy.

1. Shallow copy

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

1 ImportCopy2list_1 = [1,2,['a','b','C'],3]3List_2 =list_1[:]4 #list_2 = copy.copy (list_1) a shallow copy of another way5LIST_1[2][1] ='KK'6 Print(List_1,list_2,id (list_1[2]), ID (list_2[2]))7[1, 2, ['a','KK','C'], 3] [1, 2, ['a','KK','C'], 3]43305137364330513736

Since the shallow copy only copies the first layer of the list_1, the address of the list in the element is not changed, indicating that List_1 and List_2 share the same second-level list.

2. Deep Copy:

A deep copy completely duplicates all the data of the original variable, generating exactly the same set of content in memory, and any modification to one of the two variables will not affect the other variable.

1 ImportCopy2list_1 = [1,2,['a','b','C'],3]3List_2 =copy.deepcopy (list_1)4LIST_1[2][1] ='KK'5 Print(list_1,list_2,list_1[2],list_2[2])6[1, 2, ['a','KK','C'], 3] [1, 2, ['a','b','C'], 3]4330513736 4330512584

  

  

Assignment, shallow copy, deep copy in Python

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.