Naive idiom questions

Bjoern Schliessmann usenet-mail-0306.20.chr0n0ss at spamgourmet.com
Thu Jan 31 17:09:55 EST 2008


Terran Melconian wrote:
> * Why are there no real mutable strings available?
> 
>     I found MutableString in UserString, but further research
>     indicates that it is horribly inefficient and actually just
>     wraps immutable strings for the implementation.

Did you measure such impact on your application?

Also see http://www.skymind.com/~ocrow/python_string/
 
>     I want to be able to accumulate a string with +=, not by going
>     through an intermediate list and then doing ''.join(), because
>     I think the latter is ugly.  

>>> yummy = "ham"
>>> yummy += "eggs"
>>> yummy
'hameggs'

>     There are also times when I'd like to use the string as a
>     modifiable buffer. 

Use a list instead. When done with modifications you may concatenate
its contents to a string using "".join(my_list).

> * What's the best way to initialize a list of lists?
> [...]
> l=[[None]*5 for i in range(5)]
> 
>     I don't particularly like it, though.  It bothers me to have
>     to explain list comprehensions, which are a moderately
>     advanced feature conceptually, just to initialize an array. 

Look at "lower level" HLLs. In C++, you'd have to use

int my_array[5][5];

for (int i=0; i<5; ++i)
    for (int j=0; j<5; j++)
        my_array[i][j] = -1;

Personally, I like list comprehensions much better.        

Regards,


Björn

-- 
BOFH excuse #254:

Interference from lunar radiation




More information about the Python-list mailing list