Python memory management method and deep copy, shallow copy

Simple beginning, hand-drawn ending

1. pip common tool commands:

$pip download packagename==version   

#Download the specified version module, but do not install it

$pip freeze>requirements.txt  

#Redirect existing modules in the current environment into the requirements.txt file

Scenario: It is needed when transplanting the general environment. For example, the current program needs to be executed on a new machine, but the program may depend on many packages, but the new machine does not have the packages that the program needs to depend on. Then the current environment can be upgraded in advance. Some dependent packages import a file, and then read the packages in requirements.txt on the new machine to download and install them one by one, that is, the following command

$pip install -r requirements.txt

$pip list 

#List currently installed modules

$pip install packagname==version 

#Download and install the specified version of the package online,

$pip install package1,package2  

#Install multiple packages in sequence

$pip install --upgarde packagename

Upgrade the installation package

$pip uninstall  packagename==version  

#Uninstall the specified version module

2. The meaning of python variable assignment: and memory management method

Python is a memory management method based on value storage. So what is value-based memory management?

It means: multiple variables point to a value, then only one copy of this value will be stored in the memory, the following example: x and y variables are both equal to 3, then there will only be one copy of 3, and both x and y point to it

Understanding assignment statements in the analysis process in the following reference books:

(1) First calculate the value on the right side of the equal sign

(2) Then find a location in the memory address and store the value in it

(3) Finally create a variable and point to this memory address

Then when x=3, steps 1, 2, and 3 have been executed. When y=3, there is already 3 in the memory address, so there is no need to find another memory address to store 3, and you need to go through 1, Step 2, directly execute step 3, and point the y variable to the memory address of 3 to complete the assignment process.

So the memory addresses of x and y are the same at this time.

3. After understanding the memory management method, the understanding of deep copy and shallow copy

First come to three terminals to print the results:

① One-dimensional list: shallow copy, deep copy

② Two-dimensional list deep copy, shallow copy

Question: No matter whether it is a deep copy or a shallow copy of a one-dimensional list, it does not affect each other, but the situation of deep copy and shallow copy of a two-dimensional list is different, so why?

The following is a personal hand-drawn understanding map, and the answer should be revealed

 

The room is really cold, and the writing is too ugly, so I will just read it. The lower right corner of the picture is my personal official account. If you have any questions, you can leave a message or follow the official account to communicate.

Guess you like

Origin blog.csdn.net/Jolting/article/details/88168173