string: __iter__()?
Michele Simionato
michele.simionato at gmail.com
Wed Oct 4 08:40:02 EDT 2006
mrquantum wrote:
> Hello!
>
> Just for curiosity i'd like to know why strings don't support the
> iteration protocoll!? Is there some deeper reason for this?
>
> >>> hasattr('SomeString', '__iter__')
> False
>
> In Python 2.5 it's actually simple to obtain one:
>
> >>> myIter = (c for c in 'SomeString')
> >>> myIter.next()
> 'S'
>
> Thanks for info!
>
> Chris
Well, I see it as a feature. Typically I want to consider a string as
an atomic object (and
not as a sequence of characters) and I can check hasattr(obj,
'__iter__') to distinguish
(for instance) a list of strings from a single string (typically in
recursive algorithms
working on texts).
Michele Simionato
More information about the Python-list
mailing list