[Python-Dev] Slice as a copy... by design?

Maciej Fijalkowski fijall at gmail.com
Mon May 26 20:45:59 CEST 2008


> Hi!
>
> A thread in PyAr raised the question that, considering that strings
> are immutable, why a slice of a string is a copy and not a reference
> to a part of that string.
>
> I couldn't answer why, so I'm asking here...Is it because the
> reference counting will be complicated? Is it because it'd be
> inefficient in other way? It's something else? Or is something that
> could be done... but is not done yet?
>
> Thank you very much!

We implemented this feature in pypy. It's turned of by default, because
nobody could come out with a real world example benefiting from it.
Regarding worst-case scenario, we only create this special object
if slice length is not shorter than some percentage of source string
(I think 75%,
can be tuned).

You still need to pay for a dispatch (even if small price) wherever
you use string,
to check if this is special object or not.

Cheers,
fijal


More information about the Python-Dev mailing list