Python assignment, shallow copy, deep copy

Source: Internet
Author: User
Tags shallow copy

One, Assignment (Assignment)
>>> a = [1, 2, 3]>>> B = a>>> print (ID (a), id (b), sep= ' \ n ') 139701469405552139701469405552

  

In Python, assigning a variable to another variable is actually adding a "label" to the object currently in memory.

As in the example above, by using the built-in function ID (), you can see that a and B point to the same object in memory. a is bwill return True.

Second, shallow copy (shallow copy)

Note: The difference between a shallow copy and a deep copy is simply that for a composite object, the so-called composite object is an object that contains other objects, such as lists, class instances. In the case of numbers, strings, and other "atomic" types, there is no copy that says that the original object's references are generated.

The so-called "shallow copy" refers to the creation of a new object whose content is a reference to the elements in the original object. (Copy a Composition object without copying sub-objects)

Common shallow copies are: Slice operations, factory functions, Copy () Methods for objects, copy functions in the Copy module.

>>> a = [1, 2, 3]>>> B = List (a) >>> print (ID (a), id (b))          # A and b different identities 140601785066200 1406017847 64968>>> for x, y in Zip (A, B):       # But they contain the same     sub-object identity ... Print (ID (x), id (y)) ... 140601911441984 140601911441984140601911442016 140601911442016140601911442048 140601911442048

  

Third, deep copy

The so-called "deep copy" refers to creating a new object and then recursively copying the child objects contained in the original object. A deeply copied object has no association with the original object.

Deep copy has only one way: the Deepcopy function in the copy module.

>>> import Copy>>> A = [1, 2, 3]>>> B = copy.deepcopy (a) >>> print (ID (a), id (b)) 1406017 85065840 140601785066200>>> for x, y in Zip (A, b): ...     Print (ID (x), id (y)) ... 140601911441984 140601911441984140601911442016 140601911442016140601911442048 140601911442048

  

Looking at the example above, one might wonder:

Why are deep copies used, and the IDs of elements in A and B are the same?

A: This is because for immutable objects , when a new object is needed, Python may return a reference to an object that already exists with a type and a value that is consistent. And this mechanism does not affect the mutual independence of A and B, because when two elements point to the same immutable object, one of the assignments does not affect the other.

We can use a list of mutable objects to show exactly the difference between a "shallow copy" and a "deep copy":

>>> import Copy>>> A = [[1, 2],[5, 6], [8, 9]]>>> B = copy.copy (a)              # shallow copy get b>>> C = Copy.deepcopy (a)          # deep copy gets c>>> print (ID (a), id (b))           # A and b different 139832578518984 139832578335520>> > for x, y in Zip (A, B):        # A and B are the same     child objects ... Print (ID (x), id (y)) ... 139832578622816 139832578622816139832578622672 139832578622672139832578623104 139832578623104>>> print (ID (a), ID (c))           # A and C are different 139832578518984 139832578622456>>> for x, y in Zip (A, C):        # A and C's sub-objects are also different     ... Print (ID (x), id (y)) ... 139832578622816 139832578621520139832578622672 139832578518912139832578623104 139832578623392

  

From this example, you can clearly see the difference between a shallow copy and a deep copy.




Summarize:

1, assignment: Simply copy the object's reference, two objects have the same ID.
2. Shallow copy: Creates a new composite object that shares the child objects in memory with the original object.
3, deep copy: Create a new composite object, and recursively copy all the child objects, the new composite object has no association with the original object. Although immutable sub-objects are actually shared, they do not affect their mutual independence.

The difference between a shallow copy and a deep copy is simply that for a composite object, the so-called composite object is an object that contains other objects, such as lists, class instances. In the case of numbers, strings, and other "atomic" types, there is no copy that says that the original object's references are generated.

Python assignment, shallow copy, deep copy

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.