Open xcombelle opened 11 years ago
Python caches integers in the range -5 at 256 see this gist: https://gist.github.com/ashwin/2567864
is easy to prove because 2 * 10 is not 1024 but at the same time 2 * 10 equals 1024 are different instances of the same value
>>> id(2**10)
21285688
>>> id(1024)
21285616
>>>
I does know that in cpython it will work for integers between -5 and 256. But I think that relying on a such implementation dependent code is very brittle. The other examples are pretty idiomatic python. I think this particular example deserve at least a warning.
It depends on the interpreter and the value. Definitely not code you would want to see in the wild.
That might be don't give the expected result.
If it gives the right result is dependent of the python implementation and on cpython it depends how big is 2. if it's bigger than 255 (I believe) it will not work as expected
example: