Interpreting Python Strings and Substrings

Does python create a completely new line (copying the contents) when you perform a substring operation, for example:

new_string = my_old_string[foo:bar] 

Or does he use internment to point to old data?

As an explanation, I'm curious if there is a common character buffer, as in Java. I understand that strings are immutable and always seem like a whole new string, and this should be a brand new string object.

+4
python
Nov 03 '09 at 2:15
source share
5 answers

A study of the source shows:

When the slice indices match the beginning and end of the original row, the original row is returned.

Otherwise, you will get the result of the PyString_FromStringAndSize function, which takes an existing string object. This function returns the interned string in the case of a string with 0 or 1 character width; otherwise, it copies the substring to the new string object.

+8
Nov 03 '09 at 2:40
source share

You may also be interested in islice, which provides a source string representation

 >>> from sys import getrefcount >>> from itertools import islice >>> h="foobarbaz" >>> getrefcount(h) 2 >>> g=islice(h,3,6) >>> getrefcount(h) 3 >>> "".join(g) 'bar' >>> 
+8
Nov 03 '09 at 3:05
source share

This is a completely new line (so the old one can be released more when possible, instead of staying alive just because a small line has been cut from it, and it is stored around).

intern is quite another.

+2
Nov 03 '09 at 2:17
source share

It looks like I can answer my question, opened the source and guessed what I found:

  static PyObject * string_slice(register PyStringObject *a, register Py_ssize_t i, register Py_ssize_t j) ... snip ... return PyString_FromStringAndSize(a->ob_sval + i, ji); 

.. and no reference to internment. FromStringAndSize () only explicitly puts on lines with sizes 1 and 0 Therefore it seems obvious that you will always get a completely new object and they will not use any buffers.

0
Nov 03 '09 at 2:27
source share

In Python, strings are immutable. This means that you will always receive a copy for any fragment, concatenation or other operations.

http://effbot.org/pyfaq/why-are-python-strings-immutable.htm is a good explanation of some reasons for immutable strings.

-2
Nov 03 '09 at 2:17
source share



All Articles