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

Chris Angelico rosuav at gmail.com
Sat Apr 21 06:42:41 EDT 2018


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! :)

ChrisA


More information about the Python-ideas mailing list