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

metal metal29a at gmail.com
Fri Oct 30 01:53:49 CET 2009


On 10月30日, 上午8时49分, Chris Rebert <c... at rebertia.com> wrote:
> On Thu, Oct 29, 2009 at 5: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 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

True, '1'*1000 IS NOT '1'*999+'1'.



More information about the Python-list mailing list