4 ms·
Speaking of CPython optimizations > x = 124236 > y = 124237 > y -= 1 > id(x) 140474467900408 > id(y) 140474467900360 > x is y False > x = 1 > y = 2
by halayli 15y ago
Speaking of CPython optimizations
> x = 124236
> y = 124237
> y -= 1
> id(x)
140474467900408
> id(y)
140474467900360
> x is y
False
> x = 1
> y = 2
> y -= 1
> id(x)
140558102245816
> id(y)
140558102245816
> x is y
True
- garindra 15y agoThis is because all integers between -5 and 256 in CPython are "cached", so they all have the same identity (hence the True return value of the `is` of the second comparison -- it's between the range). http://stackoverflow.com/questions/306313/python-is-operator-behaves-unexpectedly-with-integers http://stackoverflow.com/questions/306313/python-is-operator...
- sparky 15y agoCPython interns ints in [-5, 256]. Seems like a reasonable common-case optimization to me.
- halayli 15y agoAgreed. It didn't surprise me much, but it makes things less consistent.