Deleting the first element of a list

Alex Martelli aleax at aleax.it
Thu Oct 3 10:19:05 EDT 2002


Paul Rubin wrote:
        ...
>>     bigstring = ''
>>     while somecondition():
>>         bigstring += onemoretinypiece()
>> 
>> but it doesn't matter -- it's still the wrong idiom to use in
>> Python, as its big-O behavior is a disaster.
> 
> Don't you think that's kind of a deficiency in Python?  Python could
> fix that by having mutable string objects, either under or over the
> covers.

It appears to me that immutable strings achieve both simplicity and
efficiency for an _extremely_ common and important set of cases.
Making all strings mutable would play havoc with Python's powerful
use of dictionaries for instance attributes &c.  Introducing a new
"mutablestr" type would basically offer yet one more way to perform
the same kind of tasks.  Specialcasing a += b, when a has only one
outstanding reference, to allocate excess space, would potentially
waste much memory if nothing further were then appended to a.

All in all, I suspect all solutions would be worse than the "disease"
they're supposed to cure, which is just one of a zillion uses of
strings, not the single most common one, and one which newbies soon
learn good alternative idioms for.  There is probably no way to do
strings that's just perfect for ALL uses -- at least not if you
include "newbies can't be tempted to use a very inefficient idiom"
among the criteria for perfection -- and I love the set of design
choices that Python's strings are based on.


Alex




More information about the Python-list mailing list