Troubleshooting Variable and Memory in Python

Tram Ho

Preamble

First of all, I have a lot of questions about memory and variables stored in Python. Sometimes it makes me feel like I don’t understand anything more clearly than other languages ​​I’ve studied like C or Java. So when I first encountered these questions, I was able to run the code on my device to see what it looked like (I didn’t dare to run the code in my head. ), use pages to visualize so you can learn more about how to run and save their text. Let’s start.

Sentence 1: Copy a list in Python

This question is almost found in all Python tests.

What would list b be like in this situation?


THE ANSWER IS: b WILL NOT CHANGE! Explanation: deepcopy means it creates an object copy, inserting the objects found in the original into it! So any changes to the original object will not affect the object that is created after copying (here b).

Here is the answer

The picture above clearly describes DEEP COPY.

By the way, I would like to add more about SHALLOW COPY


Explain how the Shallow copy works: build a new object then insert it references for objects found in the original.

The above image has the answer.)

A very important note: DIFFERENCE BETWEEN THE COPY OF AGRICULTURE AND DEEP WHEN ANY RELATED TO THE COMBINED OBJECTS (OBJECTS OF SUBJECTS) AS A LIST … OR CLASS INSTANCES-

Specifically, the child list in the parent list as in the example above a[2] : ['CR7', 'Messi']


As for the example below, the difference is not there:


Question 2: Regarding assignment in Python

Question :


The answer will be: Both a and b result in: [1, 2, 3, ‘日本語’, ‘English’].

Explain: Imagine that b will create a separate list: a, b can be changed freely and independently. But no, the change in both effects on each other.

In terms of memory: When assigning a variable (a = [1,2,3]), we refer it to a memory address. Then a is a type of variable pointing to the memory address of the new object (is [1,2,3]) And as shown below both point to the same memory cell !!

Sentence 3: Relates to changing strings


We will ask the question: Why a, b, as well as the top question, refer to a memory cell: Why when b changes and a does not change the same as the above question. This is explained by Mutable vs Immutable Objects in Python. String is Immutable, so it doesn’t allow changing its own value. The value at the original memory cell is b: 0x7f6d21920fb8 will remain valid forever: ‘jose’.

After the command b + = ‘mourinho’, the address of the destination cell b has changed:: 0x7f6d21920fb8 -> 0x7f6d1f91eb30

As for the List example above, it allows us to change its own value (at the memory cell itself). Because the list is Mutable objects. See the picture below:

Obviously, in memory of b, the value has been changed, and the address of the cell has not changed.

Question 4: Regarding comparisons == and IS


Explain: == is the operator comparing values ​​of 2 operands, is the operator that checks whether both operands refer to the same object or not.

Here a and b have equal values ​​but do not refer to the same object.

Closing:


Mutable vs Immutable Objects is a very important part read here: https://medium.com/@meghamohan/mutable-and-immutable-side-of-python-c2145cf72747

The Python document (most standard) is here: https://docs.python.org/3/library/copy.html

Website to visual Python code: http://www.pythontutor.com/visualize.html#mode=edit

Deeper about strings in Python: https://medium.com/@daniel.tooke/will-python-intern-my-string-94ea9efc18b2

Share the news now

Source : Viblo