Dynamic typing in Python

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.
  • Insert picture description here
  • 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

Insert picture description here
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

Insert picture description here

  • Variable a and variable b both refer to the same object, that is, point to the same memory space.
    Insert picture description here
  • 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.
    Insert picture description here

Shared reference and equality

Insert picture description here

  • "==" 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
    Insert picture description here
  • 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
    Insert picture description here

Guess you like

Origin blog.csdn.net/m0_49262457/article/details/113339336