0

I came across different behavior of 'is' in python.

n = 50000
if n is 50000 :   #comparison is success
    print "Its 50k" #Holds true

Switch over to python 'Terminal' (command prompt)

>>> n = 50000
>>> n is 50000
False

Why is the behavior different? Version: 2.7.5+

  • `is` tests identity, not equality. See [duplicate](http://stackoverflow.com/q/132988/235698). – Mark Tolonen Feb 13 '16 at 10:33
  • 1
    Because the compiler created *one* code block in a script, and compiles each expression separately in the interactive interpreter. So in the script the compiler re-uses the `50000` object, in the interpreter it doesn't. – Martijn Pieters Feb 13 '16 at 10:35
  • 1
    But just in case this wasn't clear, *never use `is`* when you need to test for equality. – Martijn Pieters Feb 13 '16 at 10:36
  • @MartijnPieters Your comment answers his question. The proposed dupe *does not* (it only explains one part of his problem). In other words, if you remove your comment and someone stumbles on this question and then reads the dupe, will he get an answer? – user Feb 13 '16 at 10:48
  • @Fermiparadox: without a clarification stating otherwise, this is a X-Y problem. They almost certainly were confused why `is` doesn't work the way they expected it to, and are not really interested in the implementation details. – Martijn Pieters Feb 13 '16 at 10:57

0 Answers0