Python shortest and deep copy usage instances and python usage instances

Source: Internet
Author: User

Python shortest and deep copy usage instances and python usage instances

This example describes the usage of Python shortest copy and deep copy. Share it with you for your reference. The specific analysis is as follows:

>>> Person = ['name', ['savings ', 100] >>> hubby = person [:] >>> wifey = list (person) >>> [id (x) for x in person, hubby, wifey] [3074051788L, 3074061740L, 3074061996L] >>> [id (y) for x in person, hubby, wifey for y in x] [3074319552L, 3074259916l, 3074319552L, 3071099916l, 3074319552L, 3074259916l] >>> hubby [0] = 'job' >>> wifey [0] = 'jar' (['job', ['savings ', 100], ['jar', ['savings ', 100], ['name', ['savings', 100])> hubby [1] [1] = 70 >>> hubby, wifey, person (['Joe ', ['savings', 70], ['Jane ', ['savings ', 70], ['name', ['savings', 70])

A small copy creates a new object, but its object reference is indeed consistent with that of the Copied object.

If the elements of the object to be copied are of an immutable type, the changes made to this element by the Copied object are actually creating a new object.

If the elements of the object to be copied are of a variable type, the changes made to this element by the Copied object are actually modified in the memory space of the original object.

In this way, if hubby [1] [1] = 70, the corresponding value of wifey and person will change.

I hope this article will help you with Python programming.

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.