zokis / Python--Faster-Way

Python: Faster Way
http://pythonfasterway.cf/
232 stars 29 forks source link

I think a is not 2 should be removed #1

Open xcombelle opened 11 years ago

xcombelle commented 11 years ago

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
zokis commented 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
>>> 
xcombelle commented 10 years ago

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.

g2p commented 9 years ago

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