You just happened to find the breaking point if the pythons memory management for integers, I was hoping you would have known it ahead of time and were being decidedly clever
In Python, the is
operator checks for object identity. Itâs explicitly asking if the two variable names are currently assigned to the exact same object in memory.
The ==
operator, however, is for value equality. Instead of checking to see if youâre pointing to the same object in memory, itâs asking if those objectsâ values are the same.
Additionally, all numbers from -5 to 256 inclusive are singletons in Python: there is one and only one instance of any of them in the Python runtime.
There are times when you definitely want to know youâre not operating on the same object: for example, youâve got two objects that should control different network connections. However, someone may have made an assignment that shouldnât have been made. You can sort out whether itâs safe to call methods on both by using the identity operator (is
).