0
>>>s1 = 100
>>>s2 = 100
>>>s1 is s2
True
>>>b1 = 257
>>>b2 = 257
>>>
>>>b11 = b12 = 257
>>>b1 is b2
False
>>>
>>>b11 is b12
True
>>>

b1 and b2 is False because of PyLongtObject what happen on b11 and b12? any idea please help me.

jawahar
  • 85
  • 1
  • 12
  • Maybe duplicate of http://stackoverflow.com/questions/3718513/python-is-statement-what-is-happening – Ax. Oct 17 '16 at 17:26
  • Or duplicate of http://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is-in-python – Ax. Oct 17 '16 at 17:27
  • sory I just made a small mistake. – jawahar Oct 23 '16 at 17:50
  • see this better understanding click [here](https://jawahar273.gitbooks.io/into-python-for-students/content/know-this/question-7.html) – jawahar Oct 23 '16 at 18:03

1 Answers1

1

It's a (convoluted) duplicate of About the changing id of a Python immutable string.

During evaluation phase in REPL loop only one constant with value 257 is created in memory.

compile("a = b = 257", '<stdin>', 'single').co_consts  # (257, None)

When executing, same object (with same address in memory) is assigned to both names.

>>> dis.dis(compile("a = b = 257", '<stdin>', 'single'))
  1           0 LOAD_CONST               0 (257)
              3 DUP_TOP             
              4 STORE_NAME               0 (a)
              7 STORE_NAME               1 (b)
             10 LOAD_CONST               1 (None)
             13 RETURN_VALUE        

Since both names point to same object, it's expected that id on those objects returns same number, therefore is returns True.

Community
  • 1
  • 1
Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93