Strings are combined in Python

Does Python have a pool of all strings and are they (strings) singleton?

More precisely, in the following code, one or two lines were created in memory:

a = str(num) b = str(num) 

?

+8
python string memory singleton string-interning
Mar 25 '10 at 21:33
source share
3 answers

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 # <-- this will print False in most cases (but try str(1) is str(1)) 

But when using constant strings directly in your code, CPython uses the same string instance:

 a = "text" b = "text" print a is b # <-- this will print True 
+16
Mar 25 '10 at 21:40
source share

In general, strings are not interned in Python, but sometimes they look like this:

 >>> str(5) is str(5) True >>> str(50) is str(50) False 

This is not uncommon in Python, where regular objects can be optimized in such a way that they are unusual:

 >>> int(5+0) is int(5+0) True >>> int(50+0) is int(50+0) True >>> int(500+0) is int(500+0) False 

And keep in mind that all of these details will differ between Python implementations and even between versions of the same implementation.

+5
Mar 25 '10 at 21:40
source share

Lines are not interned at all. In your example, two lines will be created (except for values ​​from 0 to 9). To test this, we can use the is operator to see if two lines are the same object:

 >>> str(1056) is str(1056) False 
+1
Mar 25 '10 at 21:35
source share



All Articles