Why don't strings share data in Python?

Tim Peters tim.one at comcast.net
Tue Apr 16 02:10:02 EDT 2002


[Mike Coleman]
> Does anyone know why strings (i.e., those of length >1) don't
> share their data in Python?

There are many reasons, but one of them is invisible to Python users:
because Python is intended to play nice with C libraries, all Python strings
end with a \0 byte.  There's nothing you can do in pure Python code to
confirm or deny that, but it's true anyway under the covers.  Tons of (C)
code relies on that for simplicity and speed.

Somtimes it peeks thru in a bug; e.g., this very old bug just got fixed a
day or two ago:

>>> complex("1+1j\0buncha junk")
(1+1j)
>>>

It reveals that the internal code that parses a string passed to complex()
mistakenly stopped at the first \0 byte instead of paying attention to the
*length* of the string (which Python stores separately from the string
guts).






More information about the Python-list mailing list