[Python-Dev] Death to string functions!
Guido van Rossum
guido@python.org
Tue, 19 Dec 2000 10:37:15 -0500
> So I was thinking about this whole thing, and wondering why it was
> that seeing things like:
>
> " ".join(aList)
>
> bugged me to no end, while:
>
> aString.lower()
>
> didn't seem to look wrong. I finally put my finger on it, and I
> haven't seen anyone mention it, so I guess I'll do so. To me, the
> concept of "join" on a string is just not quite kosher, instead it
> should be something like this:
>
> aList.join(" ")
>
> or if you want it without the indirection:
>
> ['item', 'item', 'item'].join(" ")
>
> Now *THAT* looks right to me. The example of a join method on a
> string just doesn't quite gel in my head, and I did some thinking and
> digging, and well, when I pulled up my Smalltalk browser, things like
> join are done on Collections, not on Strings. You're joining the
> collection, not the string.
>
> Perhaps in a rush to move some things that were "string related" in
> the string module into methods on the strings themselves (something I
> whole-heartedly support), we moved a few too many things
> there---things that symantically don't really belong as methods on a
> string object.
>
> How this gets resolved, I don't know... but I know a lot of people
> have looked at the string methods---and they each keep coming back to
> 1 or 2 that bug them... and I think it's those that really aren't
> methods of a string, but instead something that operates with strings,
> but expects other things.
Boy, are you stirring up a can of worms that we've been through many
times before! Nothing you say hasn't been said at least a hundred
times before, on this list as well as on c.l.py.
The problem is that if you want to make this a method on lists, you'll
also have to make it a method on tuples, and on arrays, and on NumPy
arrays, and on any user-defined type that implements the sequence
protocol... That's just not reasonable to expect.
There really seem to be only two possibilities that don't have this
problem: (1) make it a built-in, or (2) make it a method on strings.
We chose for (2) for uniformity, and to avoid the potention with
os.path.join(), which is sometimes imported as a local. If
" ".join(L) bugs you, try this:
space = " " # This could be a global
.
.
.
s = space.join(L)
--Guido van Rossum (home page: http://www.python.org/~guido/)