0

I'm using Python 3.7.4 and understand the small integers are numbers [-5,256].

I want to know why the result of #2 in the code below returns True. Shouldn't it be False?

#1
b=257
c=257
b=b+1
c=c+1
print(id(b))
print(id(c))
print(b is c)

#2
b1=257
c1=257
print(id(b1))
print(id(c1))
print(b1 is c1)

Result in Visual Code Terminal
4306256464
4306256432
False
4306256240
4306256240
True

Here's my code in Mac Terminal, which I think is the ideal result

Python 3.7.4 (v3.7.4:e09359112e, Jul  8 2019, 14:54:52) 
Type "help", "copyright", "credits" or "license" for more information.
>>> b=257
>>> c=257
>>> print(id(b))
4351554224
>>> print(id(c))
4351554096
>>> print(b is c)
False

Reference: [1]"is" operator behaves unexpectedly with integers

Grace Lee
  • 11
  • 1

1 Answers1

0

Python instantiates and interns the range of numbers -5 to 256 inclusive all of which will be singleton objects. Python creates a single object for all the variables which have the same value between -5 to 256

Read Singletons and interning in Python.

accdias
  • 5,160
  • 3
  • 19
  • 31
Ashwani
  • 1,340
  • 1
  • 16
  • 34
  • Also, please post the code here instead of an image. – Ashwani Jan 05 '20 at 17:28
  • 2
    The *CPython* implementation interns small integers; the language Python says nothing about whether an implementation must or must not do any such interning. – chepner Jan 05 '20 at 17:34