It's always been in the back of my mind that directly iterable strings are
very often more trouble than they are worth. But I also agree that changing
it would probably be extremely disruptive (and also might be more trouble
than it's worth).
I've only been at it for about 3 years, but because of iterable strings I
always seem to regret not using a function like this in many contexts:
def iter_nostr(iterable):
if isinstance(iterable, str):
raise TypeError(f"iterable cannot be a str")
yield from iter(iterable)
The nature of my coding work is a lot of parsing of different kinds of text
files and so I've had to write a lot of functions that are meant to take in
an iterable of strings. So the biggest context that comes to mind is to
guard against future me mistakenly sending a string into functions that are
intended to work with iterables of strings. I always seem to find a way to
do this, with all kinds of head scratching results depending on the
situation. I learned early on that if I don't include a guard like this,
I'm going to pay for it later.
---
Ricky.
"I've never met a Kentucky man who wasn't either thinking about going home
or actually going home." - Happy Chandler
On Sat, Feb 22, 2020 at 11:09 PM Steven D'Aprano
On Sun, Feb 23, 2020 at 01:28:35AM -0000, Steve Jorgensen wrote:
Another issue is one of consistency. A string is not a sequence of length-1 strings.
Isn't it? It sure looks like a sequence of length-1 (sub)strings to me.
Of course, strings can also be views as a sequence of paragraphs, lines, words, or any other sort of substring you like. Hypertalk allowed you to read strings in arbitrarily nested "chunks":
get the last character of the first word of the third line of text
with similar syntax for writing and iteration. One might argue that there's nothing particularly useful about characters, and treating strings as sequences of words, lines etc could be more useful. But I don't think we can argue that strings aren't sequences of substrings, including characters.
The length-1 strings created by iterating are more like slices than members.
They certainly aren't members (attributes). You can't access a particular substring using dot notation:
# Give me the first char in the string c = string.0 # Syntax error.
So substrings aren't attributes/members. What would you name them? The same applies to lists and other sequences as well.
-- Steven _______________________________________________ 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/XFG4AL... Code of Conduct: http://python.org/psf/codeofconduct/