On Oct 24, 2019, at 14:13, Greg Ewing <[email protected]> wrote: > > I'm thinking of things like a function to recursively flatten > a nested list. You probably want it to stop when it gets to a > string, and not flatten the string into a list of characters.
A function to recursively flatten a nested list should only work on lists; it should stop on a string, but it should also stop on a namedtuple or a 2x2 ndarray or a dict. A function to recursively flatten arbitrary iterables, on the other hand… And I don’t think there’s any conceptual problem with strings being iterable. A C++ string is a sequence of chars. A Haskell string is a plain old (lazy linked) list of chars. And similarly in lots of other languages. And it’s rarely a problem. There are other differences that might be relevant here; I don’t think they are, but to be fair: C++ and Haskell implementations are expected to optimize everything well enough that you can just any arbitrary sequence of chars as a string with reasonable efficiency, so strings being a thin convenience wrapper above that makes intuitive sense. In Python, that isn’t true; a function that loops character by character would often be too slow to use. C++ and Haskell type systems make it a little easier to say “Here’s a function that works on generic iterables of T, but when T is char here’s a more specific function”. But again, I don’t think either of these is the reason Python strings being iterable is a problem; I think it really is primarily about them being iterables of strings. _______________________________________________ Python-ideas mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/4YF3JNZ6KYZLOCOLFHFLDSGRZL66O4SP/ Code of Conduct: http://python.org/psf/codeofconduct/
