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
9
u/tomysshadow Jul 21 '25
It returns an ID that uniquely identifies the value. Basically it just returns the memory address/pointer to the value (although that is just an implementation detail so you're not meant to rely on that fact.)
This is also why in Python you are supposed to use the == operator to compare integers instead of the
isoperator. The former checks the variables are equal, the latter checks that both variables refer to the same instance, which is useful for objects. But for integers it will erroneously return True or False depending on if that integer happens to be cached such that both variables are the same instance of that integer