string.join() syntax quirky?

Bjorn Pettersen BPettersen at NAREX.com
Mon Nov 26 16:26:33 EST 2001


> From: Chris Barker [mailto:chrishbarker at home.net] 
> 
> > > One way to look at it is that the various collection 
> classes do NOT 
> > > inherit from a single base class (which would be a 
> logical place to 
> > > define a join method). Hence to have all kinds of 
> collections learn 
> > > to "join" requires a lot of repeat coding. Making join a string 
> > > method solves this problem.
> > 
> > So instead of doing what's right we're doing what's convenient? 
> > Doesn't sound very Pythonic <wink>
> 
> Another way to look at is is that joining and splitting are 
> inherently string functionailty, and thus belong with the 
> string object. Or the string module, if you take the 
> procedural view of programming. I don't think join() belongs 
> with sequences at all, as sequences can hold anything,not 
> just strings.

And here I thought I wanted to join the elements of a list with a string
(not insert this string between them -- wouldn't I then call insert?
<wink>)

And while it's true that sequences can hold anything, I fail to see what
the relevance is in a dynamic language. I would expect to get something
like:

   TypeError: sequence item 1: expected string, int found

Which is what you get now if your list isn't of the right type...

> "".join(list) is a little clunky, but it makes sense to me.

Nah, it's just plain ugly <wink>

-- bjorn




More information about the Python-list mailing list