4

Based on this answer, we know that Python returns a reference to integers from -5 <= x <= 256. When I enter the following into an interpreter, I get the expected results:

big_num_1 = 1000
big_num_2 = 1000
print(big_num_1 == big_num_2, big_num_1 is big_num_2)

Which gives me:

True False

But if i save it inside the file myFile and simply do an import:

import myFile

It always give me the following output:

True True

Does Python handle these references differently when not run from an interpreter?

Ed Harrod
  • 3,423
  • 4
  • 32
  • 53
  • 7
    It is very much up to the interpreter and the optimisation process which numbers will be interned and which won't be. You simply should never use `is` with numbers, because there's no right answer. – deceze Mar 01 '19 at 11:35
  • 2
    @deceze my be your comment with extra example can be the answer? – Brown Bear Mar 01 '19 at 11:39
  • 1
    @Paul Thats about the same as the answer the OP himself linked to - we would need a canonical for int interning which I was unable to locate - there are plenty of posts about string interning though - but they do not quite fit this question. This is essentially a "why" - "because it is like that and implementation detail based" question that has not really a good answer. *Don't us is to compare ints* is the best answer – Patrick Artner Mar 01 '19 at 11:54
  • 2
    @BearBrown probably because interning is an implementation detail that "just is" and there are a bazillion posts that do tell you not to use **is** to compare numbers because it does not work that way. We can speculate a lot and attrackt answers but in real we won't be able to answer this above and beyond what was already covered in at least 2 high ranging questions [whats-with-the-integer-cache-inside-python](https://stackoverflow.com/questions/15171695/) and [is-there-a-difference-between == and is](https://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is) and [...] – Patrick Artner Mar 01 '19 at 11:57
  • [...] maybe [why-does-comparing-strings-using-either-== or-is-sometimes-produce-a-differen result](https://stackoverflow.com/questions/1504717/why) for the interning parts that might happen for ints (similar to strings). – Patrick Artner Mar 01 '19 at 11:58
  • @PatrickArtner thank you! – Brown Bear Mar 01 '19 at 12:11

0 Answers0