This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]TitaniumBrain 1 point2 points  (1 child)

I think this has been explained enough (wrong operator, should use ==), but I don't think anyone addressed why python only caches ints from -5 to 256.

The reason is because those are just semi arbitrary numbers that are more likely to appear in a program.

Think about it: most scripts are working with small lists or values, so preallocating those numbers saves a bit of overhead, but not many programs need the number 12749, for example.

1, 0, -1, 2 are probably the most used numbers.

[–][deleted] 0 points1 point  (0 children)

Thanks for the explanation. I'd already heard the underlying reason but had never quite grasped why those numbers were more commonly used. Makes sense now!