Why don't strings share data in Python?
Joe Mason
jcmason at student.math.uwaterloo.ca
Tue Apr 16 16:43:05 EDT 2002
In article <ku4ricgeve.fsf at lasipalatsi.fi>,
Erno Kuusela <erno-news at erno.iki.fi> wrote:
>In article <87d6x09trz.fsf at mathdogs.com>, Mike Coleman
><mkc+dated+1021521407.f909ec at mathdogs.com> writes:
>
>| Does anyone know why strings (i.e., those of length >1) don't share
>| their data in Python? Since their immutable, it seems like this
>| would be the obvious thing to do.
>
>in addition to reasons presented i other replies, this would
>also result in much less obvious memory usage. saving a short
>slice of a huge string would hang on to the huge string. java
>does this, and i've heard people make less than flattering
>comments about this feature.
I suppose it would be possible to implement copy-on-collect as well as
copy-on-write (wow, how's that for a self-contradiction?) When the GC
notices that the only reference to the string is actually a substring, it
makes a copy and throws out the original.
But, yeah, it'd be a little complicated.
Joe
More information about the Python-list
mailing list