Brett Cannon added the comment:
I'm going to close this as we try to avoid documenting CPython-specific details
unless absolutely necessary. And in the case of documenting the str type we
should avoid that.
So thanks for the suggestion, Dmitriy, but we will just leave this as a
pleasant sur
Dmitriy added the comment:
Yes, optimization is really not working in case of prepending. In case of
multiple references I couldn't get quadratic time grow.
Concerning the Windows, yes, the optimization may be not always efficient:
>>> timeit('a+="a"', setup='a=""', number=1)
0.001169099
Steven D'Aprano added the comment:
Eric is correct that this is a CPython optimization, it is not a language
feature.
Furthermore, it is an optimization that can be affected by rather subtle
factors such as the operating system memory management. Here is a thread
demonstrating that code tha
Eric V. Smith added the comment:
It's my understanding that this is a quality of implementation issue, and that
in other (non-CPython) implementations, the run time for repeated concatenation
may indeed be quadratic.
The optimization in CPython relies on knowing the reference count is 1. If
New submission from Dmitriy :
There is an error in documentation about string concatenation:
https://docs.python.org/3/library/stdtypes.html
---
Common Sequence Operations
[...]
6. Concatenating immutable sequences always results in a new object. This means
that building up a sequence by repe