converting an array of chars to a string

Duncan Booth duncan at NOSPAMrcp.co.uk
Mon Jul 8 04:07:32 EDT 2002


Derek Thomson <derek at wedgetail.com> wrote in 
news:3D1307AF.3070308 at wedgetail.com:

> I would have expected "join" to be a method on the sequence, if 
> anything. As in "s = lst.join('')". In fact, I looked for it, as I 
> *knew* I'd seen it used before as a method, I just didn't expect it to 
> be in the string class.
If join was a method on the sequence, then it would have to be implemented 
for every sequence you could join and it would have to check the type of 
the separator to find out what type to return. As a method of the 
separator, it has the appropriate implementation to return a 'str' or 
'unicode' object, and works on any sequence type (you can even join a 
string).

Writing ''.join(list) still sucks though. I've taken to writing 
str.join('', list) instead as something I find more readable.
When the separator is known in advance I sometimes wrap it up:

   joinlines = '\n'.join
   ...
   return joinlines(list)

Plenty people think that sucks though.

> I think I'll just stick with string.join for the time being. As it turns 
> out, there's more than one way to do it, even in Python ;)
Not really, as string.join simply calls the join method of the separator.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list