In python
>>> a = 5 >>> a is 5 True
but
>>> a = 500 >>> a is 500 False
This is because it stores low integers as a single address. But as soon as the numbers begin to be complex, each int gets its own unique address space. That makes sense to me.
The current implementation saves an array of integer objects for all integers from -5 to 256, when you create an int in this range, you actually just return a reference to an existing object.
So why does this not apply to strings? Are strings as complex as large integers (if not more)?
>>> a = '1234567' >>> a is '1234567' True
How does python efficiently use the same address for all string literals? It cannot contain an array of all possible strings, for example, for numbers.
source share