[Python-ideas] discontinue iterable strings

Stephen J. Turnbull turnbull.stephen.fw at u.tsukuba.ac.jp
Mon Aug 22 05:47:16 EDT 2016


Nick Coghlan writes:

 > However, the real problem with this proposal (and the reason why the
 > switch from 8-bit str to "bytes are effectively a tuple of ints" in
 > Python 3 was such a pain), is that there are a lot of bytes and text
 > processing operations that *really do* operate code point by code
 > point.

Sure, but code points aren't strings in any language I use except
Python.  And AFAIK strings are the only case in Python where a
singleton *is* an element, and an element *is* a singleton.  (Except
it isn't: "ord('ab')" is a TypeError, even though "type('a')" returns
"<class str>".  <sigh/>)

I thought this was cute when I first encountered it (it happens that I
was studying how you can embed a set of elements into the semigroup of
sequences of such elements in algebra at the time), but it has *never*
been of practical use to me that indexing or iterating a str returns
str (rather than a code point).  "''.join(list('abc'))" being an
identity is an interesting, and maybe useful, fact, but I've never
missed it in languages that distinguish characters from strings.
Perhaps that's because they generally have a split function defined so
that "''.join('abc'.split(''))" is also available for that identity.
(N.B. Python doesn't accept an empty separator, but Emacs Lisp does,
where "'abc'.split('')" returns "['', 'a', 'b', 'c', '']".  I guess
it's too late to make this change, though.)

The reason that switching to bytes is a pain is that we changed the
return type of indexing bytes to something requiring conversion of
literals.  You can't write "bytething[i] == b'a'", you need to write
"bytething[i] == ord(b'a')", and "b''.join(list(b'abc')) is an error,
not an identity.  Of course the world broke!

 > But we're not designing a language from scratch - we're iterating
 > on one with a 25 year history of design, development, and use.

+1 to that.



More information about the Python-ideas mailing list