'11' + '1' is '111'?

John Machin sjmachin at lexicon.net
Thu Oct 29 21:10:42 EDT 2009


On Oct 30, 11:52 am, Benjamin Kaplan <benjamin.kap... at case.edu> wrote:
> On Thu, Oct 29, 2009 at 8:43 PM, metal <metal... at gmail.com> wrote:
> > '11' + '1' == '111' is well known.
>
> > but it suprises me '11'+'1' IS '111'.
>
> > Why? Obviously they are two differnt object.
>
> > Is this special feature of imutable object?
>
> It's an implementation detail of small strings without spaces and
> small numbers. You're more likely to reuse those values, so Python
> caches them. You shouldn't rely on it. It's not guaranteed to stay the
> same between different implementations, or even different versions of
> CPython.

It also relies on the implementation detail that the CPython bytecode
has peephole optimisation applied to it:

| Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit
(Intel)] on win32
| Type "help", "copyright", "credits" or "license" for more
information.
| >>> def foo():
| ...    return '11' + '1' is '111'
| ...
| >>> import dis
| >>> dis.dis(foo)
|   2           0 LOAD_CONST               4 ('111')
|               3 LOAD_CONST               3 ('111')
|               6 COMPARE_OP               8 (is)
|               9 RETURN_VALUE
| >>> def bar():
| ...    a = '11'
| ...    b = '1'
| ...    return a + b is '111'
| ...
| >>> dis.dis(bar)
|   2           0 LOAD_CONST               1 ('11')
|               3 STORE_FAST               0 (a)
|
|   3           6 LOAD_CONST               2 ('1')
|               9 STORE_FAST               1 (b)
|
|   4          12 LOAD_FAST                0 (a)
|              15 LOAD_FAST                1 (b)
|              18 BINARY_ADD
|              19 LOAD_CONST               3 ('111')
|              22 COMPARE_OP               8 (is)
|              25 RETURN_VALUE
| >>> foo()
| True
| >>> bar()
| False
| >>>

In general, whether (expression1 is expression2) is true or false is
not useful knowledge when the expressions result in "scalars" like
str, int, float.



More information about the Python-list mailing list