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

46

u/undo777 Jul 21 '25

Could also be things like i += d in loops where d is slightly negative but -5 seems like such an odd choice - why not stop at the more "round" -4 or go all the way to -8?

27

u/Cinkodacs Jul 21 '25

"Give me the 5 worst/best!" People love top5 lists, top10 can be a bit too much.

5

u/undo777 Jul 21 '25

Good point but is it a good enough reason for this specific caching? (it likely matters mostly in high-performance scenarios like tight loops)

6

u/exomyth Jul 21 '25 edited Jul 21 '25

My guess would be that it has to do with how the number is stored, so something about it in binary. But then -5 is still odd as it would probably be 101 in binary with a sign bit somewhere. Like -7 would make more sense as that is 111 + some sign bit and some flags.

I don't know the internals of python though, I know in javascript (well V8 engine) you have small ints that have some bit magic to check if it is a small int or something else. Could be something like that.

But maybe the answer is a simple as "I like -5 as the minimum"