Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize Deep and shallow copy with Python

Shulou Source: shulou.com Published: 2022-06-01 19:58:48 10月19日 Update

This article mainly introduces how to achieve deep and shallow copy of Python, which is very detailed and has certain reference value. Friends who are interested must finish reading it!

Deep and shallow copy

Object reference, shallow copy, deep copy (expansion, difficulty, focus)

In Python, there is a difference between object assignment and copy (deep / shallow copy). If you don't pay attention to it when using it, unexpected results may occur.

In fact, this is the result of shared memory.

Copy: in principle, it is to separate the data, copy it, and modify it later without affecting each other.

Let's first look at a non-copy example.

Use = assignment (object reference)

= assignment: data is fully shared

= assignment is to point to the same object in memory. If it is a mutable type, such as a list, modify one of them, the other must change.

If it is an immutable type (immutable), such as a string, modify one of them, the other will not change

Shallow copy (copy)

Shallow copy: data semi-sharing (copy its data in independent memory, but only copy the first layer successfully)

The above is all the contents of the article "how to make a deep and shallow copy of Python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Copy data object depth memory content article type result variable accident success between value example interest principle character string guy Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Huawei MariaDB vpn Redmi