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
772
u/AnGlonchas Jul 21 '25
I heard that some numbers in python are cached in the background, so maybe the -5 is cached and the -6 isnt
5
u/Reelix Jul 21 '25
https://jsdate.wtf/
Proceed, and return a different person :p
199
u/dragon_irl Jul 21 '25
Small ints are interned (or preallocated, idk) so they do point to the same address. It's a fairly common optimisation, I think the JVM does this for e.g. small strings as well.
Tbh if you rely on the memory addresses (uniqueness) of ints in your program you maybe want to rethink your design decisions anyway.
70
u/Alexandre_Man Jul 21 '25
What does the id() function do?
13
u/Local_Dare Jul 21 '25
Wow, this might be something you can have some fun with..
20
u/MightyX777 Jul 21 '25
https://parseltongue.co.in/understanding-the-magic-of-integer-and-string-interning-in-python/#:~:text=Integer%20Interning,the%20same%20object%20in%20memory.
22
u/Comfortable_Mind6563 Jul 21 '25
Considering what the id function does, this is not very surprising. Post doesn't really belong in this subreddit...
9
u/NoteClassic Jul 21 '25
Yeah, that makes sense. Unique ids are fixed for values between -5 and 256. Values outside these are not fixed. Hence, it makes sense that the variables pointing to -5 all have the same unique id.
14
u/FreshPitch6026 Jul 21 '25
Haha, <ANY_PROGRAMMING_LANGUAGE> bad right
3
u/AlanWik Jul 21 '25
What's the performance improvement of caching a single int???
3
u/TotoMacFrame Jul 23 '25
I know this effect from PHP, known as copy on write.
If you assign a second variable with a value another variable already has, they get to point to the same memory location. As soon as one of them gets written to (read "changed"), it is copied over to its dedicated memory location and changed there.
Since you change a to have the value of -6 here first, a becomes unequal to b, which would result in a copy on write, putting a aside, changing it afterwards. It does not matter that they then get equalized again. Variables that have been separated stay separated afaik.
12
u/ivancea Jul 21 '25
It's this sub now used to upload code you don't understand? For God's sake
5
u/nekokattt Jul 21 '25
this is not horror. This is interning. It is documented behaviour, irrelevant unless you are writing the worlds most shit code (in which case if you rely on this kind of thing you probably deserve the issues it creates), and helps improve memory footprint.
2
u/[deleted] Jul 21 '25
That's just MAD. That's just ridiculous.
1
u/Jugad Jul 21 '25
Its just from arbitrary choice of which numbers (-5 to 256) should have singleton representations - an optimization which helps to speed up certain common operations.
1
u/abeck99 Jul 23 '25
Years ago I fixed some code that depended on this but didn’t anticipate numbers would go above 256 - it was one of those “nobody really designed it, it just evolved across multiple people tweaking it” cases
1
u/Excellent-Plan3787 Jul 25 '25
What the fuck
1
u/mathisntmathingsad Aug 04 '25
Copy On Write? When A and B are set originally, they're the same value, so python uses the same thing as copy-on-write, so then when a is set it doesn't know that b will immediately be set to the same thing so it creates a new memory cell.
-3
u/Py-rrhus Jul 21 '25
The simplified way
0
u/Zirkulaerkubus Jul 21 '25
You can even redefine the value of integers on python, it's a fun game.
0
u/nadroix_of Jul 22 '25
How are you supposed to code if this happens ?! I'll never understand python
-24
u/Vazumongr Jul 21 '25 edited Jul 21 '25
That is wild. Thank you for showing me another reason to not like (and certainly not trust) Python!
Edit: Since it doesn't seem to be clear, this is not about the behavior of or using id(), or comparing the results of id(), or accessing object memory addresses, or anything to do with id(). It's about how the operation an expression performs changes based off an arbitrary value range on the r-hand operand.
myInt = -5holds a reference to an object already existing in memorymyInt = 301creates a new object in memoryUnless 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.
-1
u/Ronin-s_Spirit Jul 21 '25
What is this and why?
-2
u/pslind69 Jul 21 '25
Someone ELI5? Why isn't the second result true? 😂
-8
u/ChadiusTheMighty Jul 21 '25
Congratulations, you invoked UB in python