0

I was doing a beginner exercise regarding identity operator "is".

In Python Shell,

Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = -5
>>> print(a is -5)
True
>>> a = -6
>>> print(a is -6)
False

And now in VScode

a = -5
print(a is -5)
a = -6
print(a is -6)

c:\Users\Desktop\ppython\practice.py:4:
  print(a is -6)
True
True

Cleary the program is processed differently in Python Shell and VScode, but I could not understand why this is the case nor which is "correct". Could someone please explain this?

  • Both are "correct". And they aren't really functioning differently, insofar as they are both functioning as documented. – juanpa.arrivillaga Dec 05 '22 at 11:53
  • TL;DR: never expect any particular behaviour of `is` with literals. Only expect `is` to behave predictably with objects which you know are unique instances, like `is None` or `a = object(); b = a; a is b`. – deceze Dec 05 '22 at 12:04
  • noted deceze, thank you ! – James Bucket Dec 05 '22 at 12:31

0 Answers0