0

In Python, x is y returns true if the two sides evaluate to the same object; that is, it checks pointer equality. Now I tried something like this:

>>>a = 2  
>>>a is 2  
True  

However..

>>>a = 9203409249024  
>>>a is 9203409249024  
False

Why this difference in behavior?

Ankur Ankan
  • 2,953
  • 2
  • 23
  • 38
Nick
  • 1,793
  • 2
  • 15
  • 13
  • 1
    This is an optimization in Cpython (and perhaps some others) and has been answered here before... I'll look for the dupe... – mgilson Jan 07 '14 at 05:34

1 Answers1

0

Python (and other languages) cache small numbers in memory, so that multiple variables that have the value (for example) 2 will point to the same location in memory (and, hence, is returns True).

Hardmath123
  • 341
  • 1
  • 9