Lines are immutable in Python, so an implementation can decide whether interns should (which is a term often associated with C #, which means some lines are stored in a pool) or not.
In your example, you are dynamically creating strings. CPython does not always look at the pool to determine if a line is already there - it also does not make sense, because you first need to reserve memory to create a line, and then compare it with the contents of the pool (inefficient for a long line).
But for strings of length 1, CPython looks at the pool (see "stringobject.c"):
static PyStringObject *characters[UCHAR_MAX + 1]; ... PyObject * PyString_FromStringAndSize(const char *str, Py_ssize_t size) { ... if (size == 1 && str != NULL && (op = characters[*str & UCHAR_MAX]) != NULL) { #ifdef COUNT_ALLOCS one_strings++; #endif Py_INCREF(op); return (PyObject *)op; } ...
So:
a = str(num) b = str(num) print a is b
But when using constant strings directly in your code, CPython uses the same string instance:
a = "text" b = "text" print a is b
AndiDog Mar 25 '10 at 21:40 2010-03-25 21:40
source share