Iteration of strings

Alex Martelli aleax at aleax.it
Sat Nov 23 08:27:40 EST 2002


Terry Reedy wrote:
   ...
>> This is unfortunate, because in a sense, a string is also a list of
> strings!
> 
> Not just 'in a sense'.  Substituting 'sequence' for 'list', this is a
> defined feature of the anguage.  If one wants the polymorphism, this
> is fortunate, not unfortunate.  I consider being able to iterate
> through strings a great feature.

Personally, I don't.  On the extremely few occasions in which I HAVE wanted 
to iterate character by character on a string, I would have had no problem 
asking for that explicitly -- be that by passing the string to the list 
constructor, or calling some other hypothetical built-in, string method, or 
whatever.  This specific piece of polymorphism is one of the few that has 
given me only trouble (albeit in modest amounts), since almost always when 
I'm dealing polymorphically with sequences I want strings to be atoms, and 
them I have to take the trouble to single them out.

It's all academic, of course, since compatibility constraints mean this 
isn't going to change, but, were this a "greenfield" language design 
situation, I'd find my sympathies going to the OP's preferences against 
strings being implicitly iterable (as long as we don't lose _slicing_ on 
strings, of course -- now THAT is quite useful a lot of the time!).


Alex




More information about the Python-list mailing list