Assignment and copy in Python

Source: Internet
Author: User

Assignment

In python, a value assignment is to create an object reference, instead of storing the object as another copy. For example:

>>> a=[1,2,3]>>> b=a>>> c=a

The object is [1, 2, 3], which is referenced by three variables a, B, and c respectively. The three variables do not exclusively occupy the object [1, 2, 3], or you can use any variable to modify the object [1, 2, 3.

>>> c.append(4)>>> c[1, 2, 3, 4]>>> a[1, 2, 3, 4]>>> b[1, 2, 3, 4]>>> b.append("from b")>>> b[1, 2, 3, 4, 'from b']>>> a[1, 2, 3, 4, 'from b']>>> c[1, 2, 3, 4, 'from b']

Copy

The copy function can be divided into shortest copy and deep copy. First, let's look at the example below.

>>> A = [1, 2, 3] # establish a reference connection between a and the object >>> B = a # by assigning values, B Also creates a reference with the object >>> import copy >>> c = copy. copy (a) # create a copy of [1, 2, 3], that is, a new object >>> d = copy. deepcopy (a) # creates a copy of [1, 2, 3.

The relationships between a, B, c, and d are as follows:

A -- |
| --> [1, 2, 3]
B -- |

C -----> [1, 2, 3]

D -----> [1, 2, 3]

Modify

>>>. Append ('A') # change the original object through variable a to [1, 2, 3, 'a'] >>> B # B also changes [1, 2, 2, 3, 'a'] >>> c # c corresponds to a copy, which is not affected [1, 2, 3] >>>> d # d same as above [1, 2, 3]

The relationships between a, B, c, and d are shown in:

A -- |
| --> [1, 2, 3, 'a']
B -- |

C -----> [1, 2, 3]

D -----> [1, 2, 3]

Modify c:

>>> C. append ("cc") # modify the object of c> a # a is not affected, because a and c point to different objects [1, 2, 3, 'A'] >>> B [1, 2, 3, 'a'] >>> c [1, 2, 3, 'cc']> d [1, 2, 3]

The relationships between a, B, c, and d are shown in:

A -- |
| --> [1, 2, 3, 'a']
B -- |

C -----> [1, 2, 3, 'cc']

D -----> [1, 2, 3]

In the above example, there seems to be no difference between copy and deepcopy, and they both create another copy. See the following example:

>>> Q = [1, 2, 3, ['A', 'B'] # note that this object is a list with a list element, q [3] = ['A', 'B'] >>> w = copy. copy (q) # w and e are references of the copy objects of the shortest copy and the deep copy respectively> e = copy. deepcopy (q) >>> q. append ('4q') # [1, 2, 3, ['A', 'B'] corresponding to q is changed to [1, 2, 3, ['A ', 'B'], '4q']> q [1, 2, 3, ['A', 'B'], '4q'] >>> w # w, e is the same as before, and is not affected [1, 2, 3, ['A ', 'B']> e [1, 2, 3, ['A', 'B']

Modify the corresponding list of w

>>> W. append (4) >>> w # adds an integer 4 [1, 2, 3, ['A', 'B'], 4] >>> q # q and e are not affected [1, 2, 3, ['A', 'B'] >>> e [1, 2, 3, ['A', 'B']> q [1, 2, 3, ['A', 'B']

Modify the elements of w [3]. The prompt is that w is a shortest copy of q.

>>> w[3].append('w3c')>>> q[1, 2, 3, ['a', 'b', 'w3c']]>>> w                               [1, 2, 3, ['a', 'b', 'w3c'], 4]>>> e[1, 2, 3, ['a', 'b']]

Carefully observe the above results and find that:

Q and w. After the list elements in the list are modified, the two are modified simultaneously. E is not affected

That is, the shortest copy creates only the outer copy, but not the inner copy. The deep copy creates a complete copy. This explains the meaning of the Chinese translation name "Shallow". Its "Shallow" means copying a layer (outer layer ).

Further modify e to see the effect:

>>> e.append('5e')>>> q[1, 2, 3, ['a', 'b', 'w3c']]>>> w[1, 2, 3, ['a', 'b', 'w3c'], 4]>>> e[1, 2, 3, ['a', 'b'], '5e']>>> e[3].append('e3c')>>> q[1, 2, 3, ['a', 'b', 'w3c']]>>> w[1, 2, 3, ['a', 'b', 'w3c'], 4]>>> e[1, 2, 3, ['a', 'b', 'e3c'], '5e']

Programmers who think about it will ask a question here. Why do we need to have a shortest copy and a deep copy? How do they work? To answer this question on the magic network, please refer to the following two connection content:

Http://blog.csdn.net/llg8212/article/details/22782387http://blog.csdn.net/yueguanghaidao/article/details/25613887

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.