Skip to content Skip to sidebar Skip to footer

Why Are Larger Int Numbers Returning Different And Sometimes The Same Ids?

Today I learned about id and decided to put it to use and test things out. I know integers are immutable, so the id should be (?) the same. But as I was testing things out in the p

Solution 1:

As an optimization, Python pre-creates a range of int objects (I think it's -5...256 by default, this is a compile-time option), and always uses those objects in preference to creating a new int. For ints outside that range, the chance of ever needing the exact same int again is considered too low to be worth the effort of checking to see if the needed int object already exists.

This is PURELY an implementation detail. If your code ever actually cares about it, you are doing something horribly wrong.

Post a Comment for "Why Are Larger Int Numbers Returning Different And Sometimes The Same Ids?"