In order for this proposal to be seriously considered, I think it's necessary to cite many realistic examples where the current behavior is problematic enough to justify changing the current behavior, and that adding a str.chars() and eventually removing the ability to iterate over strings would provide a better fix than any existing solutions. Simply stating that much of the Python community considers the current behavior to be "somewhat problematic" based on previous suggestions is certainly not going to be adequately convincing, at least not to me.

But, even in the case that we are able to conclude that the current behavior causes significant issues that can't be addressed as well with existing solutions, I strongly suspect that this is going to be the case of change that is far too fundamental to Python to be changed at this point, even with a distant deprecation warning and many years of advanced notice regarding the removal/change after that.

I could be wrong, but the level of potential breakage that would occur seems to be on the level of Python 2 => 3 bytes changes (if not worse in some ways); which I suspect very few people want to repeat. Not to mention that pretty much all of the textbooks, documentation, and tutorials that cover Python fundamentals or have examples that iterate over strings (which is very common) would have to be updated. The argument in favor of changing the current behavior at any point in time, regardless of how far away, would have to be incredibly convincing.

For the time being, I'm -1 on both adding a chars() and an eventual deprecation => removal of iterating over strings directly. At the present moment, I don't think it's been clearly established that the current behavior is even problematic or wrong in the first place, not to mention problematic enough to justify a massive breaking change.

On Sat, Feb 22, 2020 at 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/