I read somewhere around that python, for optimization target, makes some caches of small integers and strings, so I made some experimentations.
From test1.py
:
x = 150
n = int(input('number: '))
print(id(x))
print(id(n))
So I launched it:
*****@***** ~/Desktop $ python3 test1.py
number: 150
10459808
10459808
From test2.py
:
x = 'hi'
s = (input('write: ')).strip()
print(id(x))
print(id(s))
then:
*****@***** ~/Desktop $ python3 test2.py
write: hi
140124156034832
140124155359336
Why are integers optimized at run-time and strings not?
Infact if I have:
x = 'hi'
s = 'hi'
print(id(x))
print(id(s))
it returns same ids. So why are strings optimized only at compile-time?