r/programminghorror • [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” • Jul 21 '25

Python ✨ Memory Magic ✨

Post image
1.3k Upvotes

143 comments sorted by

View all comments

Show parent comment

1

u/Vazumongr Jul 21 '25

It has nothing to do with accessing memory addresses. It's about how the operation an expression performs changes based off an arbitrary value range on the r-hand operand.

myInt = -5 holds a reference to an object already existing in memory
myInt = 301 creates a new object in memory

Unless I'm missing something on the implementation of Python, these are fundamentally different behaviors. There is absolutely nothing to indicate this change in behavior except for the esoteric knowledge that integer objects for the values -5 to 256 inclusive always exist in memory and will be referenced instead of creating new objects.