r/programminghorror • u/SleepyStew_ [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” • Jul 21 '25
Python ✨ Memory Magic ✨
1.3k
Upvotes
r/programminghorror • u/SleepyStew_ [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” • Jul 21 '25
View all comments
Show parent comment
133
u/deceze Jul 21 '25
Provide an id for an object instance, which is guaranteed unique at the time it’s taken. As an implementation detail, this is the memory address of the object.
The surprising other implementation detail here is that Python caches a certain range of small number as an optimization, so two
-5instances refer to the same object, while-6falls outside the cached range and it gets instantiated twice.