Sep-21-2017, 10:01 PM
Well, you're also using small ints. Those, in particular, will have the same id... but only on cpython. Any other implementation of python (IronPython, jython, pypy, etc) might not work that way. And that's only because of an optimization the cpython compiler uses (it assumes small ints are used fairly frequently, so it just caches them and shares them all over the place).
If you try the same thing with strings, it doesn't do that anymore...
If you try the same thing with strings, it doesn't do that anymore...
# 50 is the same... >>> id(50) 1426349088 >>> id(50) 1426349088 >>> id(50) 1426349088 >>> id(50) 1426349088 # but "50" is not the same... >>> id("50") 31633984 >>> id("50") 31635328 >>> id("50") 31635296 >>> id("50") 31634464 >>> id("50") 31633920