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

Chris Rebert clp2 at rebertia.com
Fri Oct 30 01:49:12 CET 2009


On Thu, Oct 29, 2009 at 5:43 PM, metal <metal29a 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 used to optimize performance. CPython
caches certain small strings and returns the same object when asked
for a string with identical content to one in the cache, thus the
behavior you're observing.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list