1

I tried this operation in python interactive mode :

>>> (1*1) is 1
True
>>> (377*35) is 13195
False
>>> 377*35
13195
>>> 377*35 is 377*35
False
>>> 1*1 is 1
True

Could anybody explain why ' (377*35) is 13195 ' is false?

Thanks in advance!

hugle
  • 145
  • 1
  • 5

1 Answers1

3

A is B checks that A and B refer to the same object. It does not check whether A equals B numerically.

The reason for the different behaviour in your examples is that ints with small values (typically between -1 and 99 inclusive) are "interned" by the interpreter -- whenever a result has such a value, an existing short int with the same value is returned.

This explains why is returns True for your examples involving small numbers but not for those involving large numbers.

NPE
  • 486,780
  • 108
  • 951
  • 1,012