[Python-Dev] RFC: Add a new builtin strarray type to Python?
Hrvoje Niksic
hrvoje.niksic at avl.com
Mon Oct 3 10:31:07 CEST 2011
On 10/02/2011 06:34 PM, Alex Gaynor wrote:
> There are a number of issues that are being conflated by this thread.
>
> 1) Should str += str be fast. In my opinion, the answer is an obvious and
> resounding no. Strings are immutable, thus repeated string addition is
> O(n**2). This is a natural and obvious conclusion. Attempts to change this
> are only truly possible on CPython, and thus create a worse enviroment for
> other Pythons, as well as a quite misleading, as they'll be extremely
> brittle. It's worth noting that, to my knowledge, JVMs haven't attempted
> hacks like this.
CPython is already misleading and ahead of JVM, because the str += str
optimization has been applied to Python 2 some years ago - see
http://hg.python.org/cpython-fullhistory/rev/fb6ffd290cfb?revcount=480
I like Python's immutable strings and consider it a good default for
strings. Nevertheless a mutable string would be useful for those
situations when you know you are about to manipulate a string-like
object a number of times, where immutable strings require too many
allocations.
I don't think Python needs a StringBuilder - constructing strings using
a list of strings or StringIO is well-known and easy. Mutable strings
are useful for the cases where StringBuilder doesn't suffice because you
need modifications other than appends. This is analogous to file writes
- in practice most of them are appends, but sometimes you also need to
be able to seek and write stuff in the middle.
Hrvoje
More information about the Python-Dev
mailing list