3 ms·
For performance reasons, python re-uses the same objects for small integers, which are 0 to 256 if I recall right. This is done so the interpreter doesn't have
by tetha 9y ago
For performance reasons, python re-uses the same objects for small integers, which are 0 to 256 if I recall right. This is done so the interpreter doesn't have to allocate a new object whenever your code uses the rare constant "1". Object allocation is slow, after all.
Given this, the expression "1" on the current version of cpython returns the same object from the small integer cache every time. Not just an equal object, the very same object. Overall, don't depend on it - it's an optimization and can change all the time.
- bakery2k 9y ago0 to 256 if I recall right -5 to 256 (inclusive) [https://github.com/python/cpython/blob/master/Objects/longobject.c#L18 https://github.com/python/cpython/blob/master/Objects/longob...]