[Python-ideas] discontinue iterable strings

Alexander Heger python at 2sn.net
Sat Aug 20 16:56:49 EDT 2016


>
> > Yes, I am aware it will cause a lot of backward incompatibilities...
>
> Tell me, would you retain the ability to subscript a string to get its
> characters?
>
> >>> "asdf"[0]
> 'a'
>
> If not, you break a ton of code. If you do, they are automatically
> iterable *by definition*. Watch:
>
> class IsThisIterable:
>     def __getitem__(self, idx):
>         if idx < 5: return idx*idx
>         raise IndexError
>
> >>> iti = IsThisIterable()
> >>> for x in iti: print(x)
> ...
> 0
> 1
> 4
> 9
> 16
>
> So you can't lose iteration without also losing subscripting.
>

Python here does a lot of things implicitly.  I always felt the (explicit)
index operator in strings in many other languages sort of is syntactic
sugar, in python it was taken to do literally the same things as on other
objects.  But it does not have to be that way.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160821/829098bb/attachment.html>


More information about the Python-ideas mailing list