Why don't strings share data in Python?

Gerhard =?unknown-8bit?Q?H=E4ring?= gerhard at bigfoot.de
Tue Apr 16 00:43:34 EDT 2002


* Chris Gonnerman <chris.gonnerman at newcenturycomputers.net> [2002-04-15 23:21 -0500]:
> From: "Mike Coleman" <mkc+dated+1021521407.f909ec 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:]
> 
> [...]
> Are you certain Python doesn't do it?

It doesn't. There's only one optimization in
Objects/stringobject:string_slice, and it's about slicing the whole
string:

>>> x = "hello"
>>> id(x)
134867304
>>> y=x[:]
>>> id(y)
134867304

> Can you submit a patch to implement it? (PEP + Patch == Implemented
> Change in many cases).

Why a PEP? This isn't a language change, only an implementation detail.
No PEP needed IMO.

The problem with implementing this is that it wouldn't exactly simplify
garbage collection of strings ;-) If it can be efficiently and safely
implemented, then I'd like to see this change, though. I'm not sure if
this is possible, and if it's worth the effort.
 
Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id AD24C930
public key fingerprint: 3FCC 8700 3012 0A9E B0C9  3667 814B 9CAA AD24 C930
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))





More information about the Python-list mailing list