SoFunction
Updated on 2024-11-19

Python advanced tutorials of dynamic types in detail

Dynamic typing is another important core concept of Python. As we said before, Python's variables do not need to be declared, and at assignment time, variables can be reassigned to any value. These are related to the concept of dynamic typing.

dynamic type

Among the objects we come across, there is a special class of objects that are used to store data. Common objects of this class include various numbers, strings, tables, and dictionaries. In C, we call some of such data structures as variables. And in Python, these are objects.

An object is an entity stored in memory. But we do not have direct access to the object. The name of the object we write in the program is just a reference to that object.

The separation of references and objects is at the heart of dynamic typing. A reference can always point to a new object:

Copy Code The code is as follows.

a = 3
a = 'at'

In the first statement, 3 is an integer object stored in memory. By assignment, reference a points to object 3.

In the second statement, the object 'at' is created in memory as a string (string). The reference a points to 'at'. At this point, object 3 no longer has a reference pointing to it, and Python automatically destructs the object with no reference pointing to it, freeing the corresponding memory.

(For small integers and short strings, Python caches these objects rather than creating and destroying them frequently.)

Copy Code The code is as follows.
a = 5
b = a
a = a + 2

Look at the example again. With the first two sentences, we have a,b pointing to the same integer object5 (b = a means that reference b points to the same object that reference a points to). But the third sentence actually reassigns the reference a to point to a new object.7 At this point a,b point to separate objects. We see that even when multiple references are pointing to the same object, if the value of one reference changes, it actually makes that reference point to a new reference, and doesn't affect the pointing of the other references. From the effect is that each reference is independent of each other, do not affect each other.

The same is true for other data objects: the

Copy Code The code is as follows.

L1 = [1,2,3]
L2 = L1
L1 = 1

However, note the following

Copy Code The code is as follows.

L1 = [1,2,3]
L2 = L1
L1[0] = 10
print L2

In that case, we no longer assign a value to the reference L1, but to the element of the table pointed to by L1. The result is that L2 changes at the same time.

What is the reason? Because the references to L1, L2 have not changed and still point to that table. Tables are actually objects that contain multiple references (each reference is an element, e.g. L1[0], L1[1]... , each reference pointing to an object, e.g. 1,2,3), . The assignment operation L1[0] = 10 does not change the pointing of L1, but operates on L1[0], a part (an element) of the table object, so all references to that object are affected.

(In contrast, none of our previous assignment operations acted on the object itself, only changing the reference pointing.)

Lists can change the object itself (in-place change) by referencing its elements. This object type, called variable data objects (mutable object), the dictionary is also such a data type.

And like before the numbers and strings, can not change the object itself, can only change the reference pointing, called immutable data object (immutable object).

The tuple we learned earlier, although it can be called to refer to the element, but can not be assigned, and therefore can not change the object itself, so it is also considered to be immutable object.

Function parameter passing from dynamic typing

Functions are passed parameters that essentially pass references. For example:

Copy Code The code is as follows.

def f(x):
    x = 100
    print x

a = 1
f(a)
print a


The argument x is a new reference to the object pointed to by a. If the argument is an immutable object, the a and x references are independent of each other. An operation on the parameter x does not affect the reference a. Such a pass is analogous to a value pass in C.

If a mutable object is passed, then changing the function parameters may change the original object. All references to the original object will be affected, and it is important to keep an eye out for this when programming. For example:

Copy Code The code is as follows.

def f(x):
    x[0] = 100
    print x

a = [1,2,3]
f(a)
print a

Dynamic typing is one of the core mechanisms of Python. You can take your time to familiarize yourself with it in your application.

summarize

Separation of references from objects, which are entities that store data in memory, and references point to objects.

Mutable objects, immutable objects

function value passing