first, the basic principleEverything in Python is an object, and a variable is a reference to an object.
This is a universal rule. Let's give a sample. How Python is handled.
x = ' Blue ' y = ' green ' z = x
When Python runs the first sentence above, a Str object is first created in the heap with the text content blue. At the same time, a reference to an object named X is also created. X refers to this str object.
The second sentence is similar; the third one creates a new object reference named Z. and set it to the same object that the object references x points to. For example with the change.
So the function of assigning an action symbol "=" in Python is to bind an object reference to an object in memory. If the object already exists, it is simple to bind again. To refer to the object to the right of "=", assuming that the object reference does not already exist. You first create the object and then bind the object reference and the object.
Python uses the "dynamic type" mechanism, that is, in a Python program. Whenever possible, an object reference can be bound again to a different object (not requiring the same type). This is not the same as other hardened languages (C++,java), just agreeing to bind to objects of the same type again.
In Python, because there is a "dynamic type" mechanism, an object reference can run methods that are applicable to different types of objects. When an object does not exist, no matter what the object references. into the garbage collection process.
To view the type program:
Import Typestype (100)
>>> a=100>>> a/1010>>> A[0]traceback (most recent): File "<pyshell#5>", Line 1, in <module> a[0]typeerror: ' int ' object have no attribute ' __getitem__ ' >>> a= ' Hello ' >>& Gt A[0] ' h ' >>> a/10traceback (most recent): File "<pyshell#8>", line 1, in <module>
The first sentence a = 100
Python creates an int object in the heap. Its content is 100. Create a variable at the same time, whose value points to the Int object in the heap
The second sentence A/10
Python applies the "dynamic type" mechanism, inferring that the object a points to is an int and is able to apply/(divide) the operation.
The result is then calculated.
(Does it create a 10.0 object in the heap?)
)
Third sentence a[0]
Python applies the "dynamic type" mechanism, inferring that a points to an int type and does not apply [] (shard) operations. The error is then made.
Sentence a = ' Hello '
Python creates a Str object in the heap. Its content is hello. Change the A variable at the same time so that its value points to the Str object in the heap. The original int object at the same time. Because the object reference is not present. So it went into the garbage collection process.
Sentence A[0]
Python applies the "dynamic type" mechanism, inferring that the object a points to is a str type. The ability to apply [] (shard) operations. The result is then calculated.
Sixth sentence A/10
Python applies the "dynamic type" mechanism, inferring that a points to an object that is str type and does not apply/(division) operations. The error is then made.
In short : In Python Everything is an object, and the variable always holds the object reference. When an object does not have a variable pointing to it, it enters the garbage collection process. Python's "dynamic type" mechanism, which is responsible for checking the variable's object reference for the applicable action.
Assume that the object does not apply the operation. The error will be directly. Word " variable has no type. Object has type "
Second, the Python ID (). The difference between ==,is
1. ID (): Gets the address of the object in memory
2. Is: An object reference to 2 variables (the address of an object in memory.) That is, the value obtained by the ID () is the same . Assume that the same returns true. Otherwise, false is returned.
In other words, whether the object reference to 2 variables points to the same object.
3. = =: is the same as the content of the object to which the 2 variables are pointing.
One into Python deep like the sea--Variables and objects