While the two correct solutions are x is z and id(x) == id(z) I want to point out the details of the python implementation. Python stores integers as objects, as an optimization, it generates a bunch of small integers at the beginning (-5 - 256) and points to EVERY variable containing an integer with a small value, these pre-initialized objects. More information
This means that for integer objects initialized with the same small numbers (from -5 to 256), checking if the two objects are the same will return true (ON C-Pyhon, as far as I know, this is an implementation detail), while for a larger number returns true only if one object is initialized from another.
> i = 13 > j = 13 > i is j True > a = 280 > b = 280 > a is b False > a = b > a 280 > a is b True
ted Jan 28 '16 at 14:11 2016-01-28 14:11
source share