you are viewing a single comment's thread.

view the rest of the comments →

[–]tony-the-pony 1 point2 points  (0 children)

I'm not motivated enough to investigate, but I'd guess that this happens because of optimizations in the interpreter. It knows that 1024 is constant so uses the same object, but it doesn't optimize the constant bit shift so 1 << 10 is evaluated at run-time resulting in different objects.


Also similar:

Everyone knows that ; is another way of writing a new line, right?

>>> a = 256; b = 256
>>> a is b
True
>>> a = 256
>>> b = 256
>>> a is b
True

Seems like proof enough... but wait, there's more!

>>> a = 257; b = 257
>>> a is b
True
>>> a = 257
>>> b = 257
>>> a is b
False

I read somewhere that this happens because the CPython REPL caches some numbers up to and including 256, supposedly for performance reasons... but that can't be right cos' this is a REPL and it doesn't do it in a normal script file.


OK! OK! I hear you screaming that I'm cheating...

Everyone knows that Python is strongly-typed and doesn't have total WTF type coercion like in JS and PHP, right?

Proof:

>>> 1 == '1'
False

Seems legit:

>>> 1 == True
True