1
>>> a = 257
>>> b = 257
>>> a is b
False

the above code is working as expected in python shell

a = 257
b = 257
print(a is b)
True

the above is the output that prints on a python file

  • 3
    Are you sure you use the same version of Python in both cases? Add `print(sys.version)` to each fragment. – DYZ Mar 09 '20 at 06:13
  • @metatoaster The OP's question is different because the values of `a` and `b` are the same in both fragments. – DYZ Mar 09 '20 at 06:14
  • 1
    @DYZ it's irrelevant, please read the thread, multiple answers there clearly explains why this behavior is inconsistent. – metatoaster Mar 09 '20 at 06:16
  • What version of Python 3 are you using? Please provide the exact version! @Aravind – Saurav Saha Mar 09 '20 at 06:17
  • @metatoaster There is nothing in that thread that explains why sometimes `a is b` for "big" `a` and `b` and sometimes it is not. A reference to "_unpredictable_" is not an explanation. – DYZ Mar 09 '20 at 06:24
  • 1
    @DYZ show me exactly where I referenced "unpredictable"? I used the word **inconsistent**. Again, since you want a specific answer, [please read this answer in its entirety](https://stackoverflow.com/a/28864111/2904896), and also [this one](https://stackoverflow.com/a/34964030/2904896) which talks about how `is` might return `True` (demonstrated with `dis`). – metatoaster Mar 09 '20 at 06:30
  • 1
    @DYZ Also finally, [this one](https://stackoverflow.com/a/49472348/2904896) shows clearly that assignments done at different statements will have different instances, while done at the same statement will have the same instance. The inconsistency arise from how the values are assigned based on the statement programmers used to assign then. – metatoaster Mar 09 '20 at 06:33
  • More specifically, it also has to do with how the bytecode interpreter make use of the constants - when the interpreter loads the program those constants simply get squashed. – metatoaster Mar 09 '20 at 06:41
  • @metatoaster The question is why the same implementation - if it is the same implementation - in one case creates the same instance, and in the other, it does not. If we believe the OP that the only difference between the cases is whether they were execeuted on the command line or from a file, then the difference is bizarre. – DYZ Mar 09 '20 at 06:42
  • 2
    @DYZ the difference is not bizzare - in the interactive interpreter every statement is independent from one another, while execution from the file will involve the Python compiling the constants down to one location and both assignments will be loaded from that, as per the answers shown by usage of `dis` in that thread. – metatoaster Mar 09 '20 at 06:45
  • 3
    @DYZ: the answer links metatoaster cited you answer in general: never use `x is y`/`x is not y` to compare. Your question here is claiming the exact same cPython implementation behaves inconsistently, which has not been verified; DYZ asked you to prove that with `print(sys.version)` for each fragment. Please edit that into your question. Next, doesn't [that answer](https://stackoverflow.com/a/34964030/2904896) address it? – smci Mar 09 '20 at 06:47
  • Instead of covering the OP under comments, just vote to close as a duplicate. – Antti Haapala -- Слава Україні Mar 09 '20 at 07:22

0 Answers0