"/a" is not "/a" ?

"Martin v. Löwis" martin at v.loewis.de
Fri Mar 6 17:46:14 EST 2009


> So, it appears that in the first case a and b are names to the same
> string object, while in the second case they are to two separate
> objects. Why?

This question is ambiguous:
a) Why does the Python interpreter behave this way?
   (i.e. what specific algorithm produces this result?)
or
b) Why was the interpreter written to behave this way?
   (i.e. what is the rationale for that algorithm?)

For a), the answer is in Object/codeobject.c:

        /* Intern selected string constants */
        for (i = PyTuple_Size(consts); --i >= 0; ) {
                PyObject *v = PyTuple_GetItem(consts, i);
                if (!PyString_Check(v))
                        continue;
                if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
                        continue;
                PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
        }

So it interns all strings which only consist of name
characters.

For b), the rationale is that such string literals
in source code are often used to denote names, e.g.
for getattr() calls and the like. As all names are interned,
name-like strings get interned also.

> What's so special about the forward slash that cause the
> two "/a" strings to create two separate objects?

See above.

> Is this an implementation-specific issue?

Yes, see above.

Martin



More information about the Python-list mailing list