Python--Faster-Way icon indicating copy to clipboard operation
Python--Faster-Way copied to clipboard

I think a is not 2 should be removed

Open xcombelle opened this issue 12 years ago • 3 comments

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:

>>> print(2**10 is not 1024)
>>> print(2**10 != 1024)
True
False

xcombelle avatar Sep 21 '13 15:09 xcombelle

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
>>> 

zokis avatar Sep 24 '13 15:09 zokis

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.

xcombelle avatar Oct 01 '13 18:10 xcombelle

It depends on the interpreter and the value. Definitely not code you would want to see in the wild.

g2p avatar Dec 30 '14 12:12 g2p