Tell the sister about python-S01E10 dynamic type and shared reference

WeChat public number: Python data scientist

[Points to see first]

1.python uses a dynamic type mechanism2. Python variables are essentially pointers to memory space3.python garbage collection mechanism4.Python shared reference phenomenon

[Sister said] I have been having doubts since this time. I can see the expression of int a = 1 in C language, but why didn’t I see this in Python?

To answer this question, you have to talk about object reference mechanisms and dynamic types in Python.

Indeed, Python does not declare the type of a variable when it uses variables, which is different from C. However, variables can also work, because in Python types are automatically determined during the run, not by code, which means there is no need to declare variables in advance.

In Python, we have to clarify a concept: variable names and objects are divided The variable name never has any associated type information, the type is associated with the object, and does not exist in the variable name. A variable name is created the first time it is assigned, and when a new assignment expression appears, it is immediately replaced by the currently newly referenced object. This is what Python calls the dynamic type mechanism. . Specifically look at an example:

a = 'abcde'print(a)a = [1,2,3,4,5]print(a)abcde[1, 2, 3, 4, 5]

Combined with the simple example above, let’s take a closer look:

1. Create a string object ‘abcde’, then create a variable a that concatenates the variable a with the string object ‘abcde’.

2. Then create a list object [1, 2, 3, 4, 5], and then connect him to a.

This connection from a variable to an object, which we call a reference, is implemented as a pointer in memory. So straightforwardly, internally, a variable is actually a pointer to the object’s memory space, and the object pointed to can change with the program’s assignment statement.

To sum up: variable names have no type, only objects have types, and variables simply reference different types of objects. Each object contains two headers, a type identifier, a type identifying the object, and a reference counter that indicates how many variable names are referenced by the object. If no variable references it at this time, Then you can recycle this object.

Based on the reference mechanism mentioned above, let’s talk about Python’s garbage collection mechanism.

As in the above example, whenever a variable name is given a new object, the space occupied by the previous object is reclaimed if it is not referenced by other variable names or objects. This mechanism for automatically reclaiming object space is called Garbage collection mechanism.

That is, when a is assigned to the list object [1, 2, 3, 4, 5], the memory space of the string object is automatically reclaimed (provided that if he is not referenced by another variable)

The specific internal mechanism is this: Python keeps a counter in each object that records the current number of references to the object. Once this counter is set to 0, the memory space of this object is automatically reclaimed. When a is assigned to the list object, the reference counter of the original string object ‘abcde’ becomes 0, causing his space to be reclaimed. This makes it unnecessary for us to write code that frees memory space like C++.

[Sister said] Then talk about the contents of the shared reference:

As shown below, multiple variable names refer to the same object, called For shared references

a = 'abcde'b = aprint(a)print(b)abcdeabcde

At this point, the reference count of the string object ‘abcde’ is 2, let’s look further down. If we reassign the variable a at this time?

a = 'abcde'b = aa = [1,2,3,4]print(a)print(b)[1, 2, 3, 4]Abcde

The result is obvious, the variable a becomes a reference to the list object, and the variable b is still a reference to the string object ‘abcde’, and the reference count of the string object is changed from 2 to 1.

If b is reassigned at this time, the reference count of the string object ‘abcde’ will be 0, and the object will be garbage collected.

[Sister said] This episode feels very detailed, let me sum up: assign a new value to a variable, not to replace the original object, but to let the variable refer to another object that is completely different, and the original object The reference count is decremented by 1. Well combing these concepts, it’s very useful~