Dynamic type
In the objects we touch, there is a special kind of object, which is used to store data, and the common objects include various numbers, strings, tables, dictionaries. In the C language, we call these data structures variables, and in Python, these are objects.
An object is an entity that is stored in memory. But we don't have direct access to that object. The object name that we write in the program is just a reference to the object (reference).
The separation of references and objects is the core of the dynamic type. The reference can point to a new object at any time:
A = 3'python'
In the first statement, 3 is an integer object stored in memory, and by assignment, reference a points to object 3.
The second statement, in memory, establishes the object 'Python', which is a string. Reference a points to 'python'. At this point, Object 3 no longer has a reference to it. Python automatically destroys objects that are not referenced to (destruct) and frees the corresponding memory.
(For small integers and short strings, Python caches these objects, rather than creating and destroying them frequently.) )
>>> a = 5>>> B = a # Let reference b point to the object referred to by reference a # re-assign to reference a Print aprint b5
In the above example, with the first two sentences, we let A, B point to the same integer object 5, but the third sentence actually assigns a value to reference a, so that a points to a new object 7. At this point, A/b points to different objects. We see that even if multiple references point to the same object, if a reference value changes, the reference is actually made to point to a new reference and does not affect the point of the other reference . In effect, each reference is independent and unaffected.
The same is true for other data objects:
>>> L1 = [1]>>> L2 = L1print L1print l2[, 2, 3]
However, the following conditions should be noted:
>>> L1 = [L2]>>> L1print l1[, 2, 3] Print l2[10, 2, 3]
In this case, we no longer assign a value to the L1 reference, but instead assign a value to the element of the table that L1 points to, and the result is that L2 also changes at the same time.
What is the reason? Because the direction of L1,L2 has not changed, it still points to that table. The table is actually an object that contains multiple references (each of which is an element, such as l1[0],l1[1] ..., each of which points to an object , such as a-to-one), and l1[0] = 10, this assignment is not a change in the direction of the L1, but a l1[0 ], which is a part of the Table object (an element) , so all references to that object are affected.
A list can alter the object itself (in-place change) by referencing its elements. This type of object, called a mutable data Object (Mutable object), is the same type of data as the dictionary.
And like the numbers and strings before, you cannot change the object itself, only the reference point is changed, called the immutable data Object (immutable objects).
The tuple (tuple) we learned earlier, although it is possible to invoke a reference element but not to assign a value, cannot alter the object itself, so it is also considered immutable object.
The parameter passing of function from the view of dynamic type
The argument of a function is passed, essentially a reference:
def f (x): x=100 Print x >>> a=1>>> F (a)print a1
The parameter x is a new reference to the object referred to by a. If the argument is an immutable (immutable) object, the A and x references are independent of each other. The operation on parameter x does not affect reference a. Such a pass-through is similar to the value passed in the C language
If you pass a mutable (mutable) object, changing the function parameter may change the original object, and all references to the original object will be affected.
def f (x): x[0]=100 Print x >>> a=[1,2,3]>>> F (a) [2, 3]print a[100, 2, 3]
Python Learning note 17 (dynamic type)