Why " ".some_string is often used ?

Gary D. Duzan gduzan at bbn.com
Mon Jan 12 13:16:08 EST 2004


In article <slrnbvtrsp.nh1.ramen at lackingtalent.com>,
Dave Benjamin  <ramen at lackingtalent.com> wrote:
>
>The argument is more of a technical issue. There are only two kinds of
>strings. There are many kinds of "iterables". So, it's easier to define
>"join" on the string, and force implementers of custom string types to
>implement "join" as well (since this is more rare) than to define "join" on
>an iterable and force implementers of the many kinds of iterables to define
>"join" as well. Conceptually, I'm not sure that the case is so strong that
>"join" is a string method.
>
> [ ... ]
>
>Sometimes, object-orientedness gets in the way, and I think this is one of
>those cases. "str.join" is probably the winner here, but since it's really
>just a string method being used "out of context", the delimeter is the first
>argument, and this doesn't read well to me. I think that "string.join" makes
>more sense; it says "join this sequence using this delimeter" instead of
>str.join's "join using this delimeter this sequence".

   Why not something really simple which does something like this?

def myjoin(seq,sep):
    def _addsep(l, r, s=sep): return l+s+r
    return reduce(_addsep, seq)

>>> myjoin(['a','b','c'], ",")
'a,b,c'
>>> myjoin(['a','b','c'], "")
'abc'
>>> myjoin([1,2,3,4], 0)
10
>>> myjoin("abcd", ',')
'a,b,c,d'

   It might not be the fastest, but it is straightforward and generic,
and could be optimized in C, if desired.

					Gary Duzan
					BBN Technologies
					A Verizon Company





More information about the Python-list mailing list