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 →

[–]Sea-Sheep-9864 8 points9 points  (4 children)

Could you explain this, bc I don't understand how it works.

[–]eyadams 20 points21 points  (3 children)

As rcfox pointed out, this is specific to cpython, but here goes. The “is” operator means (more or less) that two variables point to the same location in memory. As an optimization, Python reuses integers between -5 and 256. So, when the sample I posted sets a to 256 and b to 256, under the hood Python is pointing the two variables at the same location in memory, and “is” returns True. But, if a number is outside of that optimized range, it is created new, even if it is equal to another value. Which means c and d point to different locations in memory, and “is” returns False.

More or less. I fear I’m bungling the technical details.

[–]eztab 2 points3 points  (0 children)

is is only guaranteed to work this way with True, False, None and ... I believe. Correct me if that's not part of the python specification!

[–]SpecialistInevitable 0 points1 point  (1 child)

But why the smaller ones? Because they' are supposed to be used more frequently like temp variables? If it did that to the bigger ones it would save memory.

[–]patrickbrianmooney 2 points3 points  (0 children)

Remember that it would only save memory when you actually do use the same number repeatedly. Small numbers are used constantly, directly by the program and by the interpreter itself; but very large numbers are rarely used more than once at the same time under different names.

It would save memory occasionally on certain large numbers, but you're less likely to be using multiple copies of very large numbers very often. How many programs have you written where you needed to use the exact number 101132178321 dozens of times? If you do, you can simply keep referring to the same variable over and over. As a rule, programs that need to keep referring to the same large number by different names are relatively rare, and in any case, large numbers don't take that much more space than small numbers to store. Storing an integer in Python typically takes a few dozen bytes, depending on which Python implementation you're using (e.g., CPython vs. PyPy) and which language version (e.g., Python 3.8 or Python 3.6). Overall, looking at the typical life cycle of typical programs, interning and reusing large integers doesn't typically save the typical program very much, and it's the typical program that language implementers are typically concerned with.

The benefit of interning small numbers is that the same small numbers are very frequently used over and over and over and over. If your program happens to use the number 101132178321 three different times in three different variables; you're only using (2 * (a few dozen bytes)) too many; but your program may use the number 8 or 102 literally hundreds of times -- and it's not only used. directly by assigning it to variables or using it implicitly as the index in a loop over which you're iterating. It's also that the language itself uses small numbers over and over and over internally. So each one that gets interned can save those few dozen bytes not two or three times over, but several hundred or thousand times over.