Seemingly odd 'is' comparison.

Arnaud Delobelle arnodel at googlemail.com
Mon Feb 18 13:26:10 CET 2008


On Feb 13, 10:19 pm, Tobiah <t... at tobiah.org> wrote:
> >>> print float(3.0) is float(3.0)
> True
> >>> print float(3.0 * 1.0) is float(3.0)
> False

[You don't need to wrap your floats in float()]

>>> def f():
...     return 3.0 is 3.0, 3.0*1.0 is 3.0
...
>>> f()
(True, False)
>>> import dis
>>> dis.dis(f)
  2           0 LOAD_CONST               1 (3.0)
              3 LOAD_CONST               1 (3.0)
              6 COMPARE_OP               8 (is)
              9 LOAD_CONST               3 (3.0)
             12 LOAD_CONST               1 (3.0)
             15 COMPARE_OP               8 (is)
             18 BUILD_TUPLE              2
             21 RETURN_VALUE

As you can see when "3.0 is 3.0" is evaluated the same float object is
put on the stack twice so the 'is' comparison is True (LOAD_CONST 1 /
LOAD_CONST 1 / COMPARE_OP 8).

Whereas when "3.0*1.0 is 3.0" is evaluated, *two* different float
objects are put on the stack and compared (LOAD_CONST 3 / LOAD_CONST
1 / COMPARE_OP 8).  Therefore the result is False.

HTH

--
Arnaud




More information about the Python-list mailing list