Search code examples
pythonspecificationscpython

Is string slice-by-copy a CPython implementation detail or part of spec?


Python does slice-by-copy on strings: Does Python do slice-by-reference on strings?

Is this something that all implementations of Python need to respect, or is it just a detail of the CPython implementation?


Solution

  • Copying underlying memory on str slices is chosen because reference counting / garbage collection becomes complicated otherwise, and reference counting itself is an implementation detail of CPython. Therefore copying string slices is also an implementation detail.

    Copying could, in theory, be avoided in the implementation without violating any requirements: some years ago a patch was proposed attempting to avoid copying, but it was rejected by Guido because of performance concerns (ref).

    Note that already, some particular slices do not copy in CPython. This is also an implementation detail/optimization and not documented in the language reference.