Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is not documented in the language reference, and is a detail of the CPython implementation. Some years ago a patch was proposed attempting to avoid copying, but it was rejected by Guido because of performance concerns (ref).

The copy could in theory be avoided in the implementation without violating any requirements, but it would be difficult to implement with the current reference counting and garbage collection techniques used by CPython.

Copying underlying memory on str slices is done because reference counting becomes complicated without the copy, and reference counting itself is an implementation detail of CPython. Therefore copying string slices is also an implementation detail.

Note that already, some particular slices do not copy. This is also an implementation detail/optimization.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading