I have fairly frequently written some kind of recursive descent into collections. Like many people, I've had to special case strings, which are pseudo-scalar, and I don't want to descend into.

But one thing I don't think I've ever tripped over is descending into single characters, but then wanting those not to be iterable. I understands how code could hit that, but for me the scalar level has always been a whole string, not it's characters.  Yes, of course I've looped over strings, but always with the knowledge there is no further descent.

Changing strings is just too huge a change in Python semantics. But we could create a new type ScalarString or AtomicString that was "just like strong but not iterable." I'm not sure if it would subclass string, but it wouldn't directly change the string type either way.

This new type does not need to live in the standard library, but it could. Conversion to and from AtomicString would be on the user, but it's easy enough to code.

On Sat, Feb 22, 2020, 7:28 PM Steve Jorgensen <stevej@stevej.name> wrote:
From reading many of the suggestions in this list (including some of my own, of course) there seem to be a lot of us in the Python coder community who find the current behavior of strings being iterable collections of single-character strings to be somewhat problematic. At the same time, trying to change that would obviously have vast consequences, so not something that can simply be changed, even with at a major version boundary.

I propose that we start by making a small, safe step in the right direction so we can maybe reach the bigger goal in a far future release (e.g. 5.x or 6.x).

The step I propose is to add a `chars` method that returns a sequence (read-only view) of single-character strings that behaves exactly the same as `str` currently does when treated as a collection. Encourage developers to use that for iterating over characters in a string instead of iterating over the string directly. In maybe Python 4 or 5, directly using an `str` instance as a collection could become a warning.

BTW, while adding `chars`, it might also be nice to have `ords` which would be a view of the string's character sequence as `ord` integer values.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/WKEFHT4JYCL2PMZ5LB6HJRLVP3OGZI56/
Code of Conduct: http://python.org/psf/codeofconduct/