Dynamic Type:
In Python, types are automatically determined during the run, not by code.
variables:
A variable is an element of a system table that has a connection to an object
In Python, the concept of a type is present in the object, not the variable, and the variable is generic.
Use of variables:
When a variable appears in an expression, it is immediately replaced by the currently referenced object, regardless of the type of object.
and must be assigned before all variables are used.
Example:
A=3
1. Allocate a piece of memory to represent object 3;
2. Create a variable A;
3. Connect the variable and object 3;
object:
1. When the object is allocated a piece of memory, there is enough space to represent the value they are representing.
2. Object = value + header information. There are two header information: one is a type marker, and the other is a reference counter.
3. For small integers and strings, Python caches and re-uses (that is, create a 42, if the reference counter of object 42 is 0, it does not recycle garbage immediately, but a temporary place, waiting for other variable references)
Garbage collection mechanism for objects:
When a reference counter on an object is 0 o'clock, it is automatically recycled
Shared references:
In Python, assigning a new value to a variable does not replace the original object, but rather allows the variable to refer to another new object.
Immutable types:
A=3
B=a
A=a+2
1. Create object 3, variable a refers to object 3;
2. As previously stated, when a variable appears in an expression, it is immediately replaced by the currently referenced object, regardless of the type of object.
So when b=a, the direct substitution to b=3, at this time A and B together refer to the object 3;
3. The same as 2,a=a+2 directly as A=3+2=5, at which point a refers to the new object 5, and B or Reference object 3.
Variable Type:
a1=[1,2,3]
A2=a1
A1[0]=4
When executing a1[0]=4 this sentence, because the list is mutable, so the object [All-in-one] is modified directly to [4,2,3], at this time the application of A1,A2 has not changed,
So A1 change, A2 also changed.
(If you don't want this to happen, you'll need Python to copy the object instead of creating the reference, such as The Shard a2=a1[:], (if it's a dictionary, use the object. Copy ()),
At this time A1 change, A2 is not changed, because A2=A1[:],A1,A2 is not a reference to a copy of the object, but another copy of a new object [three-way], and then let A2 reference it
)
Dynamic types of Python