The is
operator will determine whether two objects have the same identity (in low-level terms, whether their representations are at the same address in memory).
a = object()
a is a
True
In Python, numbers are objects just like anything else, so whether is
works will depend on how you created them.
a = 0
a is a
True
The reason that your code works in one case and not in the other is that Python interns small integers; that is, rather than creating a new number object for -5
it will keep a cache of small integers and give you the appropriate number object. On the other hand in your case -10
is not interned so Python has to create a new number object each time.
Other objects that are interned include short strings (including single-character strings), and True
and `False.
You should not be relying on interning; instead of is
you should use the ==
operator.