Dynamic typing
Variables, objects and references
- A variable is created when it is assigned. It can refer to any type of object, but it must be assigned before being referenced.
- Variables are elements of the system table and have space for links to objects.
- Objects are allocated pieces of memory, and there is enough space to represent the values they represent.
- References are automatically formed pointers from variables to objects.
- Types belong to objects, not variables.
Object garbage collection
Each time x is assigned to a new object, and the reference counter of the previous object becomes zero, it will cause its space to be reclaimed.
Share references and modify in place
- Variable a and variable b both refer to the same object, that is, point to the same memory space.
- In the original modification, L1 and L2 refer to the same object, so changing the value of L1 also changes the value of L2.
- To make the value of L2 unchanged, copy can be used.
Shared reference and equality
- "==" Tests whether two referenced objects have the same value;'"is" tests whether two variable names refer to the same object exactly.
- If the variable name reference values are equal, but are different objects, it will return False
- x and y should be "==" (have the same value), but not is (the same object). However, because small integers and strings are cached and reused, is tells us that x and y refer to the same object