(Apr-01-2017, 10:27 PM)Nirelg Wrote: They are both int after all, so why when I write twice 2**2 it gives me the same id yet when I do the same for 2**11 it gives them different ids?Integers between -5 and 256 are cached in Python.
This optimization strategy makes sense because small integers pop up all over the place,
and given that each integer takes 24 bytes, it saves a lot of memory for a typical program.
So
id()
show memory address.>>> help(id) Help on built-in function id in module builtins: id(...) id(object) -> integer Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. (Hint: it's the object's memory address.) >>> id(10) 491765920 >>> id(10) 491765920 >>> id(10) 491765920 >>> id(100) 491767360 >>> id(100) 491767360 >>> id(100) 491767360 >>> # Now over 256 >>> id(1000) 58284576 >>> id(1000) 51896640