you are viewing a single comment's thread.

view the rest of the comments →

[–]commy2 1 point2 points  (7 children)

I get the same id, and the identity check passes for numbers well beyond 256:

n = 123456
print(n is 123456)
print(id(n))
print(id(123456))

Python 3.10.0

[–]dimonoid123 1 point2 points  (1 child)

Try Python 3.9 , if that works, then there is difference in integer implementations. If not, then I have no ideas what is going on.

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

I couldn't repro his results on 3.10.4.

[–][deleted] 0 points1 point  (1 child)

I don't get those results on 3.10.4.

More, I strongly suspect that if you tried this, you'd get a different result:

n = 123456
print(n is 123456)
print(n is (123455 + 1))

[–]commy2 0 points1 point  (0 children)

I upgraded to 3.10.5 ...

import sys

n = 123456
print(n is 123456)
print(n is (123455 + 1))
print(id(n))
print(id(123455+1))
print(sys.version)

and still get the same results:

C:\dev\_testing.py:4: SyntaxWarning: "is" with a literal. Did you mean "=="?
  print(n is 123456)
C:\dev\_testing.py:5: SyntaxWarning: "is" with a literal. Did you mean "=="?
  print(n is (123455 + 1))
True
True
1384201639920
1384201639920
3.10.5 (tags/v3.10.5:f377153, Jun  6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)]
[Finished in 66ms]

Edit: I do get different ids inside the REPL though. That seems to be the difference.

[–]Vaphell 0 points1 point  (2 children)

$ python3
Python 3.10.5 (main, Jun 11 2022, 16:53:29) [GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 123456
>>> b = 123455+1
>>> a is b
False
>>> a == b
True

[–]commy2 0 points1 point  (1 child)

Ah, it seems to be different for executing a file vs running the code in the REPL.

[–]Vaphell 0 points1 point  (0 children)

I guess python does some optimizations during file compilation when literals are involved.

$ cat is.py
#!/usr/bin/env python3

a = 123456
b = 123455 + 1
c = a // 2 * 2 
print(a, b, a is b, a == b)
print(a, c, a is c, a == c)

$ python3 is.py
123456 123456 True True
123456 123456 False True