I am getting different output for same code in VS Code and Jupyter Notebook. Both are running same version of Python 3.11.5 My code is pretty simple:
x = 257
y = 257
print(x is y)
Output in Jupyter Notebook is False while VS code, outputs True. I have tried it both on Windows and Mac but same result.
Can someone please explain me why ?
The difference is due object interning
in Python. Small numbers are cached so they point to same memory location.For numbers outside this range,new objects are created.
In VS Code optimizations may result in x is y return True
for 257 because both variables refer to the same object.In Jupyter Notebook different objects might be created so x is y returns False
To check equality use ==
, not is
.