I'm going to assume you already know why the integer values in a
, b
, c
and d
are reused (they are interned like all small integers).
The Python bytecode compiler stores immutable literals (such as floats) as constants with the bytecode. When you use the same literal more than once in a row, the same constant is reused. That means that e
and f
are both assigned the same bytecode constant; the same float
object is used for both and e is f
is True:
>>> import dis
>>> assignment = compile('''\
... e = 10.0
... f = 10.0
... ''', '<stdin>', 'exec')
>>> dis.dis(assignment)
1 0 LOAD_CONST 0 (10.0)
3 STORE_NAME 0 (e)
2 6 LOAD_CONST 0 (10.0)
9 STORE_NAME 1 (f)
12 LOAD_CONST 1 (None)
15 RETURN_VALUE
>>> assignment.co_consts
(10.0, None)
Note how both LOAD_CONST
instructions use the same index (0
) to load the floating point object; the constants structure contains just the one float
object.
However, the Python interactive session compiles each statement separately; the bytecode compiler doesn't get a chance to re-use the constant, and two distinct objects are stored instead. By the time f = 10.0
is compiled, e = 10.0
has been compiled and executed already, and the codeobject for that assignment is already gone.
This is why you see a difference between running the script in one go (as the Sublime Text Build command does) and pasting the code into the Python interactive session.