Why don't strings share data in Python?

Michael Robin me at mikerobin.com
Tue Apr 16 00:31:02 EDT 2002


If think you could ask the same thing for tuples (also r/o), or even for
lists, etc., if
you had copy-on-write semantics. (Like a "hashed lisp".) I assume the answer
is that it would take
extra time to see if the string already existed, and space to hold pointers
to strings you had no way of knowing would ever need to be referenced again.
You can always intern() strings yourself if you know your particular program
will take advantage of multiple references.

m

"Mike Coleman" <mkc+dated+1021521407.f909ec at mathdogs.com> wrote in message
news:87d6x09trz.fsf at mathdogs.com...
> 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.  So, for example, the space behavior of this code could be
linear
> rather than quadratic/horrific:
>
> d = {}
> for i in xrange(100000000):
>     d[mybigstring[i:]] = mybigstring[i:]
>
>
> Mike





More information about the Python-list mailing list