[Python-ideas] str.startswith taking any iterator instead of just tuple

Andrew Barnert abarnert at yahoo.com
Mon Jan 6 01:38:03 CET 2014


On Jan 5, 2014, at 11:02, Alexander Heger <python at 2sn.net> wrote:

>> People have mentioned use cases for iterating strings in this thread. And it's easy to think of more. There are all kinds of algorithms that treat strings as sequences of characters. Sure, many of these functions are already methods on str or otherwise built into the stdlib, but that just means they're implemented by iterating the string storage in C with a loop around "*++s". And if you want to extend that set of builtins with similar functions, how else would you do it but with a "for ch in s" loop? (Well, you could "for ch in list(s)", but that's still treating strings as iterables.) For example, many people are asked to write a rot13 function in one of their first classes. How would you write that if strings weren't iterables? There's no way a regex is going to help you here, unless you wanted to do something like using re.sub('.') as a convoluted and slow way of writing map.
> 
> whereas the issue seems now settled, you could use explicit functions
> like str.iter(), str.codepoints(), str.substr(), ...

Sure, and we could add list.iter(), list.slice(), etc. and get rid of iterables, indexing and slicing, entirely. If we add separate map and similar methods to every iterable type, we can even get rid of iterators. If it's good enough for ObjC, why should Python try to be more readable or concise?


More information about the Python-ideas mailing list