0

From doing a google search, the range of numbers where integer interning occurs in python3 is -5 to 256 but when I tried to use numbers outside of that range it still seems that integer interning is still occurring. For example:

x = 258
y = 258

print("Address of x: ", id(x))
print("Address of y: ", id(y))
print(x is y)

results in:

Address of x:  2380522692112
Address of y:  2380522692112
True

I get a similar result if I change x and y to 20000

I was expecting the address of x and y to be different and to get "False" on the third line, similar to the results that someone from the following older post got: Memory optimization / Interning in python

Has the limit for integer interning changed? If yes, does anyone know what the new range is? I'm using python version 3.11.1 by the way.

-Thanks in Advance

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
VigoHolo
  • 1
  • 1
  • 2
    This has nothing to do with interning, or with integers at all - if two literal values used in the same compilation unit (one script file, or one line of interactive mode) are equal, they get represented by the same object. – jasonharper May 16 '23 at 18:59
  • 3
    Why would you *expect* anything at all? The python interpreter reserves the right to do anything with immutable objects. One of these implementation details is the small integer cache (which doesn't always work btw, you can generate new objects in that range!). That doesn't mean there aren't *other implementaiton details* that might result in the same object. For example, constant folding in a code block... – juanpa.arrivillaga May 16 '23 at 18:59
  • 1
    It is very, very important to understand **there is no limit**. These are *implementation details* that you *shouldn't be aware of ideally* – juanpa.arrivillaga May 16 '23 at 19:00
  • So... consider: `x = (1337, -99); y = (1337, -99); print(x is y)` – juanpa.arrivillaga May 16 '23 at 19:02
  • thanks for replying. I'm new to python and based on what my professor mentioned and the previous posts that I linked to in my question, I thought there was a specific limit, but this doesn't seem to be the case. I'm still confused by why others think there was a limit on integer interning the first place. – VigoHolo May 19 '23 at 04:36
  • It used to be that way. Remember, Python has a very, VERY long history. I've been using Python since version 1.4 in roughly 1995, half a lifetime ago. Many things that "we all know" have been tweaked since then. – Tim Roberts May 19 '23 at 04:43
  • thanks Tim. integer interning still happens but it's mostly noticeable when running python through the terminal or a REPL, instead of an IDE like VS Code which I was using when I made posted the question. In the IDE, the compiler uses the same object for values that are equal like jasonharper mentioned. But when running through a REPL, each lines of code are evaluated separately thus making it easier to see the limits for integer interning. The following link explains this. https://stackoverflow.com/questions/55347581/why-does-the-is-operator-behave-differently-in-a-script-vs-the-repl – VigoHolo May 20 '23 at 02:43

0 Answers0