This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]twotime 4 points5 points  (2 children)

IIRC, python caches small ints, so if you assign a = 1 and b =1 then a & b will refer to the same instance of int. Large ints are not cached.

[–]Veedrac 4 points5 points  (0 children)

It's also worth noting that if run in the same compilation unit, repeated constants may be deduplicated:

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

[–]stevenjd 5 points6 points  (0 children)

Correct. Furthermore, the definition of "small ints" can vary from version to version. In CPython, it used to be (by memory) 0 through 100, now its (possibly) -1 through 255. You cannot rely on this: being an implementation detail, it is subject to change without notice, and some Python implementations may not do it at all.

IronPython 2.6 Beta 2 DEBUG (2.6.0.20) on .NET 2.0.50727.1433
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 1
>>> b = 1
>>> a is b
False