“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

The 30th official Python column, stop! Don’t miss this zero-based article!

The committee showed dict concepts and common operations earlier, and talked about how a new dict object generated by a shadow copy is affected by the original dict object.

In this article we’ll continue to look at the deep copy,

What is deep copy?

We can make a copy of the dictionary by using copy, but it is a shallow copy, that is, a copy but not a full copy

Shallow copy simply means copying the key, but the new dictionary’s value is still referenced by the old dictionary’s value.

In contrast to shallow copy, deep copy means that, in addition to copying keys, the new dictionary value does not use the old dictionary value reference, but creates similar data, reference separate!

That is, the memory allocation of two Spaces in tandem with the block of data, can be done without correlation!

Let’s look at the dictionary depth copy comparison code example:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @time: 2021/11/6 11:25 PM
# @Author : LeiXueWei
# @csDN /Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : thecopy.py
# @Project : hello

import copy  # Introduced the copy module, which comes with 3.8

info_dict = dict()
info_dict['name'] = 'LEI_XUE_WEI'  # or change to 'levin'
info_dict['blogs'] = '95'  # has posted 93 blog posts so far.
info_dict['gongzhong_hao'] = '[Lei School Committee]'  # Welcome to follow and support
info_dict['meta-data'] = dict(a)print('info_dict:', info_dict)

print("*" * 16)
Shadow Copy: full but incomplete Copy
shadow_copied_dict = info_dict.copy()  Copy. Copy (info_dict)
print("shadow_copied_dict :", shadow_copied_dict)

deepcopied_dict = copy.deepcopy(info_dict)
print("deepcopied_dict :", deepcopied_dict)

# Let's modify the original dict
info_dict['meta-data'] ['date'] = '06 Nov 2021'
print('info_dict:', info_dict)
print('shadow_copied_dict:', shadow_copied_dict)  The output here sees that changes are also passed to shadow_copied_dict
print('deepcopied_dict:', deepcopied_dict)  The output here sees that changes are also passed to deepCopied_dict
Copy the code

The effect is as follows:

From here, we see that two copies of the dictionary, the shallow copy, are updated because the copied dictionary has made changes.

Deep copy deepCopied_dict objects don’t move!

The copy module should also be mentioned here.

A little bit about how copy. Deepcopy works

Given the basic series of columns, this one will be a little harder to understand, but I’ll cover it in more detail in a future article.

This is only done for dict copies:

  1. Deepcopy internally looks up dict copers through the _deepcopy_dispatch object
  2. Copy methods defined by the copier traverse the original dictionary object
  3. Deep copy key and value in sequence

That’s it. If key and value are not the simplest types (such as int/float/ STR, etc.), keep looking for the copier and iterate until the base type returns directly, then backtrack.

Small white can skip this section to continue to learn, first the foundation skilled.

conclusion

There are many techniques involved in deep copy and shallow copy.

This article only introduces how to do deep copy (borrowing the copy module), but also focuses on showing and using the introduction

How do you implement it if you don’t?

Of course, the copy module also gives us a reference answer, but this involves pickle serialization, deserialization and memory management, etc. It is not difficult but longer to talk about later.

By the way, if you like Python, please check out the Committee’s Python Basics column or Python Getting Started to Master column

Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to wechat, like support collection!