In python, values of integers less than 256 are assigned to different variables. The addresses of variables in the memory are the same. Values of integers greater than or equal to 256 are assigned to different variables, the address of the variable in the memory is different. I still don't know the reason after thinking about it for a long time.
>>> Q = 255 >>> W = 255 >>>> ID (q) 34227776 >>> ID (W) 34227776 >>>> A = 256 >>>> S = 256 >>>> ID (a) 34227764 >>> ID (s) 34227764 >>>> z = 257 >>>> x = 257 >>>> ID (z) 41770536 >>> ID (x) 41770740
Python has the concept of a small integer pool. The number in the small integer pool is a common value (you can also set the parameter range by yourself. The default value is 256 ), python provides this small integer pool to prevent the program from continuously applying to release small integers, thus affecting performance.
To put it bluntly, each number in the small integer pool is an object, which exists with the python process and is irrelevant to the running program. If you want to use a value in its range, it provides it, instead of allocating space to create an object.
Integers within a certain range. They have only one unique copy.