Fixing Python's string .join()

The thing that has always irritated me about string .join() is that it doesn't stringify its arguments; if one of the things in the sequence to be joined isn't a string, .join() doesn't call str() on it, it just pukes. This is periodically annoying and inconvenient.

It recently occurred to me that this can be fixed, like so:

class StrifyingStr(str):
  def join(self, seq):
    s2 = [str(x) for x in seq]
    return super(StrifyingStr, self).join(s2)

def str_join(js, seq):
  return StrifyingStr(js).join(seq)

(A similar version for Unicode strings is left as an exercise for the reader.)

You might think that a generator expression would be more efficient than a list comprehension here; in fact, that's what my first version used. Then I actually timed it, and found out that regardless of whether or not .join() was passed a list or an iterator, and for sizes of the list (or iterator) from 10 elements to 10,000, doing the list comprehension was slightly faster.

Now that I have this I can think of a number of places where I may wind up using it, which kind of makes me wish I'd scratched this irritation before now.

These are my WanderingThoughts
(About the blog)

GettingAround
Full index of entries
Recent comments

This is part of CSpace, and is written by ChrisSiebenmann.

* * *

Atom feeds are available; see the bottom of most pages.

This is a DWiki.
(Help)

Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web

Search:
Written on 21 February 2007.
(Previous | Next)

Page tools: View Source, Add Comment.
Search:
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Wed Feb 21 22:51:33 2007
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.