[Python-ideas] Checking interned string after stringobjects concat?

Paul Moore p.f.moore at gmail.com
Sat Apr 21 06:49:16 EDT 2018


On 21 April 2018 at 11:42, Chris Angelico <rosuav at gmail.com> wrote:
> On Sat, Apr 21, 2018 at 8:25 PM, Yinbin Ma <mayinbing12 at gmail.com> wrote:
>> Hi all:
>>
>> I notice that if concatenating two stringobjects, PVM will not check the
>> dictionary of interned string. For example:
>>
>>>>> a = "qwerty"
>>>>> b = "qwe"
>>>>> c = "rty"
>>>>> d = b+c
>>>>> id(a)
>> 4572089736
>>>>> id(d)
>> 4572111176
>>>>> e = "".join(["qwe","rty"])
>>>>> id(e)
>> 4546460280
>>
>> But if concatenating two string directly, PVM would check the dictionary:
>>
>>>>> a = "qwerty"
>>>>> b = "qwe"+"rty"
>>>>> id(a)
>> 4546460112
>>>>> id(b)
>> 4546460112
>>
>> It happens in Py2 and Py3 both.
>> Is it necessary for fixing this bug or not?
>>
>
> What you're seeing there is actually the peephole optimizer at work.
> Your assignment to 'b' here is actually the exact same thing as 'a',
> by the time you get to execution. If you're curious about what's
> happening, check out the dis.dis() function and have fun! :)

To clarify, though, this is not a bug. The language doesn't guarantee
that the two strings will have the same id, just that they will be
equal (in the sense of ==).

Paul


More information about the Python-ideas mailing list