3 ms·
CPython does this too (`is` is roughly the same as Java's `==`): >>> a = 300 >>> b = 300 >>> a is b False >>> a = 100 >>> b = 100 >
by tekknolagi 4y ago
CPython does this too (`is` is roughly the same as Java's `==`):
>>> a = 300
>>> b = 300
>>> a is b
False
>>> a = 100
>>> b = 100
>>> a is b
True
>>>
CPython has a small set of canonical objects for integers.
- bjarneh 4y agoLuckily you don't typically compare numbers in Python with 'is'. In Java you should not compare Integer objects with '==' either of course, but '==' is typically used with numbers, and the auto-boxing features of Java makes it very easy to shoot your self in the foot with this stuff. I once ran into a bug which had been there for years, since some Integer values had never crept over that magic limit, and auto-boxing allowed the numbers to be treated as int's all over, so it really took me some time to figure out what was going on...