A brief introduction to the assignment & shallow copy & deep copy in Python (example)

Source: Internet
Author: User
Tags first string shallow copy
This article brings you a brief introduction to the assignment & shallow copy & deep copy of Python (example), has certain reference value, the friend who needs can refer to, hope to be helpful to you.

When writing the dict data type, there are references to the "assignment statement" and the copy () function.
In fact, when it comes to shading, you need to consider specific data types. part of an immutable object, such as a number, a string, and a mutable object, such as a list, a dictionary, etc.

I. Definition and interpretation

    • variable: an element of a system table that points to an object's connection space.

    • object: The allocated piece of memory that stores its actual value.

    • reference: A pointer to an object from a variable.

    • Immutable objects: once created, they cannot be modified, such as numbers, strings, or tuples.

    • mutable objects: objects that can be modified, such as lists, dictionaries.

    • Assignment: implemented by the statement "=". The left side is the new variable, and the right side can be either direct or existing variables. is a reference to the object, Python does not copy the object, just copies the object's reference, and the new variable points to the source variable memory address.

    • Shallow copy: a copy of an object. Copy the outermost object itself, and the inner element simply copies a reference. That is, the new creation of a type is the same as the original object, the content is a reference to the original object. The shallow copy object is new, and the object's reference content is old. 】
      a shallow copy is implemented in several ways:(1) slices: [:]; (2) factory functions such as List (), Dict (), and (3) using copy ().

    • deep copy: both the outer and inner elements are copied to the object itself, not the reference. The object is copied again, and other objects referenced in the object are also copied.

Two, immutable objects

For immutable objects, such as numbers and strings, there is no difference between an assignment (=), a shallow copy (copy ()), and a deep copy (Deepcopy ()), because their object references always point to the same memory address.
Through the example demo:

>>> var_1 = 123>>> ID (var_1)            # View address by ID () 1615552144>>> var_2 = var_1>>> ID (var_ 2) 1615552144>>>>>> import copy        # Shallow, deep copy need to import copy module >>> var_3 = copy.copy (var_1) >> > ID (var_3) 1615552144>>>>>> var_4 = copy.deepcopy (var_1) >>> ID (var_4) 1615552144

You will find that var_1 ~ Var_4 's ID point is the same.

Third, variable objects

For data types such as list, dict and so on, assignment, shallow copy, and deep copy, they change the memory address differently.
assign a shallow copy: The value is equal, the address is equal;
copy Shallow copy: value is equal, address is not equal;
deepcopy deep Copy: the values are equal and the addresses are not equal;

Example one:
base[0]= ' name ', is a string, immutable object; base[1]=[' age ', 18], is a list, mutable object.
Base_1 and base_2 are all shallow copies of base, with the same three ID ().
When a different change is made to the first string element, the discovery ID varies and does not affect each other because the string (immutable) is displayed as a copy, and when modified, a new character object is created.
When modifying the second list element, the changes are only valid for the last modification and affect each other, because the second element list simply copies its reference, modifies any shallow copy, and modifies the referenced content.

Example two: deep copy
Creates a Dictionary object and uses a deep copy to create a new object.

>>>import copy>>> var = {"A": 1, "B": 2, "C": [3, ' abc ']}>>> var1 = Copy.deepcopy (Var) >> > ID (VAR), id (var1) (17616992, 15671136)            # address is not the same >>> ID (var[' C ']), id (var1[' C ']) (15695144, 15695384)        >>> ID (var[' C '][0]), id (var1[' C '][0]) (1615550224, 1615550224)        # ' C ' The memory address of the element is the same >>>> >> var1[' C '][0] = 4>>> var{' A ': 1, ' B ': 2, ' C ': [3, ' abc ']}>>> var1{' a ': 1, ' B ': 2, ' C ': [4, ' ABC ' ]}>>> ID (var[' C '][0]), id (var1[' C '][0]) (1615550224, 1615550240)        # Changes to var1 do not affect Var
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.