[Tutor] Strange join syntax

Paul Sidorsky paulsid@shaw.ca
Fri, 26 Apr 2002 13:56:11 -0600


Scot Stevenson wrote:

> While talking about list reverse, you gave an example that included
> 
> > drow = ''.join(wordlist)
> 
> I tried this construction myself, and I see that it works, but I'm not
> sure if I understand what is happening here. My docs [Python 2.1.1, yes
> I'll be updating when I have time to install SuSE 8.0] say:

I guess the no-nonsense explanation is this:

String objects have member methods.
String constants are string objects.
------------------------------------
String constants have member methods.

You probably understand this part already but just in case, there's the
answer.  As for join itself:

> =================
> join(words[, sep]) 
> Concatenate a list or tuple of words with intervening occurrences of sep.
> The default value for sep is a single space character. It is always true 
> that "string.join(string.split(s, sep), sep)" equals s. 
> =================

I wasn't around when this debate took place but it seems logical that
the reason strings were given methods is because all of the functions in
the string module take a string to work on as their first parameter. 
All, that is, except for join().  join() works on a list, not a string. 
The only string join() takes is that optional separator, sep.  So I
guess TPTB decided that this would be what the string object would be
used for when calling join() as a method, rather than leaving join()
out.  It makes some degree of sense, even though I agree that it's a
tricky concept to grasp.

> Is there any special reason why you didn't do it this way?

Brevity, I expect, but by all means do it the other way if you find it
clearer.  The string module may be considered by some to be obsolete but
I don't think it's going to be deprecated any time soon.  

Even clearer (though much slower) would be the manual approach:

newstr = None
for s in strlist:
    if newstr is None:
        newstr = s
    else:
        newstr = newstr + sep + s

Just because shortcuts exist doesn't mean they have to be used.  :-)

> I'm amazed that the Python Elves know what do to with the '' at the
> beginning, but then I guess that's just magic for you =8).

I actually find it more amazing that the "elves" know what to do with
the dot and the function call at the end.  Directly using a literal as
an object just seems really weird to me.  Nevertheless it is quite neat,
and handy at times.

BTW you can also subscript string literals (and lists) directly, e.g.:

>>> "hey there"[2:5]
'y t'
>>> [1, 2, 3, 4][1:3]
[2, 3]

When I first saw this I thought, "Huh?  There's a subscript there but no
variable!"  Took me a few minutes to realize that the literal _was_ the
variable, and my head was spinning the rest of the day.

-- 
======================================================================
Paul Sidorsky                                          Calgary, Canada
paulsid@shaw.ca                        http://members.shaw.ca/paulsid/