2

There is the quite famous post, which states that values from -5 to 256 in Python are singletons and that using is as comparison operator will return True.

When I tried this:

c = 257
d = 257

print(c is d)
True

I expected this to be False. Currently I use Python 3.10. Did the implementation of how integers are stored change? Or did I do something wrong?

Data Mastery
  • 1,555
  • 4
  • 18
  • 60
  • Looks like they did https://docs.python.org/3/whatsnew/changelog.html "Each Python subinterpreter now has its own “small integer singletons”: numbers in [-5; 257] range. It is no longer possible to change the number of small integers at build time by overriding NSMALLNEGINTS and NSMALLPOSINTS macros: macros should now be modified manually in pycore_pystate.h header file." 257 here is inclusively, but also looks like it would depend on Python Interpreter – kboskin Oct 06 '22 at 14:39
  • 2
    While there might be an interesting tidbit to be found here, you should never be testing numbers with `is` at all, so this is a practically irrelevant implementation detail at best… – deceze Oct 06 '22 at 14:39
  • It **is** False on my interpreter in PY 3.10.4 .. not sure whats different. – rv.kvetch Oct 06 '22 at 14:39
  • @rv.kvetch: I use PyCharm with Anaconda 3.10, but the using this online editor also returned True: https://www.programiz.com/python-programming/online-compiler/ – Data Mastery Oct 06 '22 at 14:43
  • Yes, this is correct. running a python script using `python ...py` appears to return `True`, whereas the interpreter returns `False`. – rv.kvetch Oct 06 '22 at 14:44
  • @rv.kvetch: Whats the difference? Isn´t the interpreter responsible for returning the correct values in a python script?! – Data Mastery Oct 06 '22 at 14:46
  • 3
    https://stackoverflow.com/a/15172182/476 – deceze Oct 06 '22 at 14:47

0 Answers0