[Python-ideas] strings as iterables - from str.startswith taking any iterator instead of just tuple

Terry Reedy tjreedy at udel.edu
Fri Jan 3 10:23:14 CET 2014


On 1/2/2014 10:59 PM, Chris Angelico wrote:
> On Fri, Jan 3, 2014 at 2:54 PM, Alexander Heger <python at 2sn.net> wrote:
>> Generally, I find strings being iterables of characters as useful as
>> if integers were iterables of bits.  They should just be units.
>
> What this would mean is that any time you want to iterate over the
> characters, you'd have to iterate over string.split('') instead. So
> the question is, is that common enough to be a problem?
>
> The other point that comes to mind is that iteration and indexing are
> closely related.

def iter(collection): # is something like (ignoring two param form)
   if hasattr('__iter__'):
     return ob.__iter__
   elif hasattr('__getitem__'):
     return iterator(ob)

In 2.x, str does *not* have .__iter__, so the second branch is taken.

 >>> iter('ab')
<iterator object at 0x0000000002ED56D8>

In 3.x, str *does* have .__iter__.

 >>> iter('ab')
<str_iterator object at 0x00000000037D2EB8>

If .__iter__ were removed, strings would revert to using the generic 
iterator and would *still* be iterable.

> I think most people would agree that "abcde"[1]
> should be 'b' (granted, there's room for debate as to whether that
> should be a one-character string or an integer with the Unicode
> codepoint, but either way); it's possible to iterate over anything by
> indexing it with 0, then 1, then 2, etc, until it raises IndexError.
> For a string to not be iterable, that identity would have to be
> broken.

Which, to me, would be really ugly ;-).


-- 
Terry Jan Reedy



More information about the Python-ideas mailing list