Python variable declaration: The Python type belongs only to the object, not to the variable
In Python, the variable declaration does not have to specify the type of the variable as in other languages (c, C + +), the type of python is stored in the object, and the variable is simply a reference to the object, equivalent to a pointer that simply saves the address of its object. (Python object contains two header information, one is type marker, one is reference count)
Python variable assignment:
The Python variable assignment does not change the object currently pointed to, but instead changes its reference.
Such as:
1 a = # A holds 42 in-memory address 2 b = A # A is assigned to B, and the address assignment b,b and a point to the same address 3 a = # 33 assigns the address in memory to the memory address that A,a points to 33
One of the Python optimizations: Caching and Reusing objects:
Such as:
1n = 2E102A = n + 13b = n + 14 Print(A = = b)#True5 Print(A isb#False6 7n = 438A = n + 19b = n + 1Ten Print(A = = b)#True One Print(A isb#True A - -x = 42 they = 42 - Print(x = = y)#True - Print(x isY#True - +m ='ABC' -n ='ABC' + Print(M = = N)#True A Print(M isN#True
Python caches and re-uses small integers and small strings
Dynamic types of Python