On the problem
- Integer objects are immutable objects, and the result of integer operations is returned as a new object, but:
>>> a = 1 + 0
>>> b = 1 * 1
>>> id(a), id(b)
(4408209536, 4408209536)
>>> c = 1000 + 0
>>> d = 1000 * 1
>>> id(c), id(d)
(4410298224, 4410298160)
Python small integer pool creates objects with common numbers from -5 to 256 by default
-
Since the calculation result of 1 + 0 is 1, in the range of small integers, Python directly fetches the integer 1 from the static object pool; 1 * 1 is the same. The names a and b are actually bound to the same object, that is, the integer 1 in the small integer object pool, so the id is the same
-
The calculation results of 1000 + 0 and 1000 * 1 are both 1000, but since 1000 is not in the range of small integers, Python creates objects separately, so the object ids of c and d are different