If you have ever studied static language such as C,java, variables, objects, etc. need to define the type, otherwise the error
In Python, the type is dynamically set in the run
We have mentioned variables many times before, and here we say again:
We take a=3 as an example to illustrate
1. Creation of variables
First, 3 of this shaping object, and then assign the 3 object to the A variable, a variable only after initialization can be used
2. Types of variables
The variable does not have the type of information or constraints associated with it, the type is present with the object, such as the above a=3,a is no type, the type of a exists with 3 of this object, if it is a= ' a ', the type of a is a string, plainly, the type of a depends on the type of object being initialized
3. Use of variables
The variable appears in the expression, and he is immediately replaced by the object that is currently referenced, regardless of the type of object
All variables can only be used after initialization, otherwise the error
In summary: The process of a=3 is
Create an object with a value of 3, then create a variable a, and finally connect a to 3. In fact, a just refers to the memory address of the object 3
A=1a=1.1a=2 "
We are giving an example of the above code, a is a variable, a does not have a type, a=1 is the system to create an object, the value is 1, and then the address of the 1 memory block is assigned to a, or a refers to the memory address of the 1 object
The second line is, except that a now refers to the memory address of the 1.1 floating-point object, the third row, a refers to the memory address where the string object resides, a never has a fixed type, the type is present with the object, and a changes type as the object changes
Now there is a problem, when a becomes 1.1, what about the integer type Object 1? Below we introduce garbage collection of objects
An object in addition to the value, he has two header information, one is a type marker, and the other is a reference counter.
When an object is no longer referenced, the count of reference counters is 0, and this time the background collects the objects and then reclaims the destruction to free up memory space
Right here, thank you.
------------------------------------------------------------------
Click to jump 0 basic python-Catalogue
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
0 Basic python-6.1 variables, objects and references