String.join revisited (URGENT for 1.6)
Juergen A. Erhard
jae at ilk.de
Sun May 28 11:24:44 EDT 2000
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Okay, so I was using the sort method on a list of integers. And I
wondered how the *list* knows how to sort both a list of integers and
a list of strings.
The answer (okay we all know that): it doesn't. It just call cmp for
two elements, and voila... *they* know how to compare themselves to
each other.
So what's this got to do with string.join (or "string".join)?
Simple: many folks have said in the past days/weeks (since this
"string".join(list) came up) that they'd be a lot more comfortable
with s.th. like
[list].join(separator)
But others have contested this saying it would put string knowledge
into lists (or rather sequences... which do not exactly exist[1])
Well, if we'd give strings a "join" method that looked like
"some string".join("another string", "separator")
then [some list].join would simple invoke that (reduce(__join__, [some
list])). And throw an exception if the items in the list don't know
how to join themselves join.
Bye, J
PS: I fully expect for this to be too late to be changed in 1.6... I
can still hope, though. Either it happens now, or not at all... or we
need another name like "append" ;-)
PPS: Sorry, no patch, and it's probably slower than the current ugly
"string".join. But I rather choose a usable, `nice' design over speed
any time. And the speed can be improved if ".join" could take a list
of items as first argument (like "string".join(["list", "of",
"strings"], "sep")). Yes, that's almost the same as the current
string.join() ;-)
[1] I hope the (eventual, hopefully) merging of types and classes will
remedy this. Lists and tuples and strings have to be subclasses of a
sequence class (we can learn something from the Common Lisp hierarchy
I think). Oh, and that merge would also make UserList/Dict/String
obsolete, yay!
- --
Jürgen A. Erhard eMail: jae at ilk.de phone: (GERMANY) 0721 27326
My WebHome: http://members.tripod.com/Juergen_Erhard
SPACE: Above And Beyond (http://www.planetx.com/space:aab)
Codito, ergo sum - I code, therefore I am -- Raster
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.1 (GNU/Linux)
Comment: Use Mailcrypt and GnuPG <http://www.gnupg.org/>
iEYEARECAAYFAjkxOjoACgkQN0B+CS56qs3HeQCgjjEJF/7EHY41FdEv9ENoPw0e
3vcAnil3uOGlNYcKP2jRO/z3Lnz3AWjD
=OdFE
-----END PGP SIGNATURE-----
More information about the Python-list
mailing list