The is
operator is used to identify when two objects are the same - that is: internally, they are stored in the same memory position.
Python implementations are free to optimize unmutable objects - such as strings, integers and other numbers, so that when one does instantiate identical objects, the previously existing is re-used. For example, in cPython (the standard implementation), the "n" first integers (I don't remember the actual threshold) are always kept as a single instance in memory, so "is" will always be "True" for small integers.
Of course the is
operator is not meant to be used in this way. One should not care if a string or an integer is internally cached or not (although there is even a built-in function - intern
- that hints the interpreter to cache a string or object in this way).
The idea of using is
is to verify whether one object is actually the same one that you ind in another place. To consistently verify if objects are equal, one should always use the equality operator ==
.