1

is in Python tests if 2 references point to the same object. Numbers between -5 and 256 are cached internally so:

a = 10
b = 10
a is b # Results in True

How does this explain something like:

20000 is 20000 # Results in True

Both numbers are above 256. Should not the 2 integers be 2 distinct objects?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Tal
  • 322
  • 1
  • 7
  • 15
  • Note that: 1. This isn't guaranteed, so will vary between interpreters/implementations; and 2. Doesn't really make semantic sense, you want to know if the *numbers are equal*, not whether they're the same object. – jonrsharpe May 10 '16 at 16:59

2 Answers2

12

The Python interpreter sees you are re-using a immutable object, so it doesn't bother to create two:

>>> import dis
>>> dis.dis(compile('20000 is 20000', '', 'exec'))
  1           0 LOAD_CONST               0 (20000)
              3 LOAD_CONST               0 (20000)
              6 COMPARE_OP               8 (is)
              9 POP_TOP
             10 LOAD_CONST               1 (None)
             13 RETURN_VALUE

Note the two LOAD_CONST opcodes, they both load the constant at index 0:

>>> compile('20000 is 20000', '', 'exec').co_consts
(20000, None)

In the interactive interpreter Python is limited to having to compile each (simple or compound) statement you enter separately, so it can't reuse these constants across different statements.

But within a function object it certainly would only create one such integer object, even if you used the same int literal more than once. The same applies to any code run at the module level (so outside of functions or class definitions); those all end up in the same code object constants too.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • So in what cases would Python only create one object? Only if both the integers are used in the same line? Doing `a = 20000; b = 20000; a is b` results in one object, but putting the 2 assignments on 2 separate lines results in two objects. Is this really intended by Python developers? – Tal May 10 '16 at 17:07
  • 1
    @Tal: in the *interactive interpreter*, only if they are compiled together, which happens if you put everything on one line or in one compound statement. In a script (so not in the interactive interpreter), if within the same function, a class body, or at the module level. – Martijn Pieters May 10 '16 at 17:08
0

Python stores objects in memory to make things more efficient. It only needs to assign one block of memory to 20000, and so both references point to the same block of memory, resulting in True.

flybonzai
  • 3,763
  • 11
  • 38
  • 72
  • 1
    Python stores **all** objects it uses during its runtime in memory, it doesn't store these anywhere else, so your first sentence doesn't make much sense. Python will also happily create multiple `int` objects with the same value, only *literals within the same code block* are cached this way. – Martijn Pieters May 10 '16 at 17:05
  • @MartijnPieters That makes sense! – flybonzai May 10 '16 at 17:07