BOBOBK

The difference between shadowcopy and deepcopy in python

TECHNOLOGY

In python, it is common to need to copy specific objects, and you might encounter various bugs because understanding the difference between these three operations is key: assignment, shallow copy, and deep copy.

Assignment (=), shallow copy (copy), and deep copy (deepcopy) are relatively easy to distinguish regarding assignment vs. copying, but shallow copy and deep copy are harder to differentiate.

The assignment statement does not copy the object; it simply binds the variable to the object. Any change to one object will affect the other. Copying allows you to change one object without affecting the other.

The difference between shallow and deep copy is that shallow copy does not affect the other object when values change, but adding or deleting elements can affect it. Deep copy creates a completely independent object, and changes to one will not affect the other.

Below is a practical code example illustrating the differences among the three.

Code

    from copy import deepcopy
    dic1 = {'a': 2, 'b': 3, 'c': 4}
    dic2=dic1
    dic1["a"]=1
    print(dic1)
    #{'a': 1, 'b': 3, 'c': 4}
    print(dic2)
    #{'a': 1, 'b': 3, 'c': 4}
    # Assignment statement: both are exactly the same, changing one changes both
    dic1 = {'a': [1,2,3], 'b': 3, 'c': 4}
    dic2 = dic1.copy()
    dic1["a"] = [2,3,4]
    print(dic1)
    #{'a': [2, 3, 4], 'b': 3, 'c': 4}
    print(dic2)
    #{'a': [1, 2, 3], 'b': 3, 'c': 4}
    ### Changing the value does not affect the other object's value
    dic1 = {'a': [1,2,3], 'b': 3, 'c': 4}
    dic2 = dic1.copy()
    dic1["a"].append(4)
    print(dic1)
    #{'a': [1, 2, 3, 4], 'b': 3, 'c': 4}
    print(dic2)
    #{'a': [1, 2, 3, 4], 'b': 3, 'c': 4}
    ### This shows shallow copy copies elements and references to sub-objects. If the sub-object is mutable, changes affect both objects. See https://docs.python.org/3/library/copy.html for reference.
    dic1 = {'a': [1,2,3], 'b': 3, 'c': 4}
    dic2 = deepcopy(dic1)
    dic1["a"].append(4)
    print(dic1)
    #{'a': [1, 2, 3, 4], 'b': 3, 'c': 4}
    print(dic2)
    #{'a': [1, 2, 3], 'b': 3, 'c': 4}

Summary

In practical use, do not use assignment on callable objects as a copy method.

If the object does not have cyclic references or the data is not extremely large, using deep copy is safe.

Related

Recursively download files python

TECHNOLOGY
Recursively download files python

I want to back up my website recently, but the size of the file downloaded by PHP is limited, and I am too lazy to install FTP to download it. So I thought of temporarily setting up a secondary domain name site, and then using python (python3)'s requests library to directly download all the files and folders in the root directory of the website to achieve the purpose of backup.

Introduction to Artificial Neural Networks

TECHNOLOGY
Introduction to Artificial Neural Networks

Artificial Neural Network (ANN), also called Neural Network (NN) or neural-like network, is a mathematical model that mimics the structure and function of biological neural networks. It consists of a large number of neurons connected for computation. In most cases, artificial neural networks can change their internal structure based on external information, making them adaptive systems, simply put, they have learning capabilities.