yet another "Here's to 1.5.2" post

Alex Martelli aleax at aleax.it
Tue Apr 9 13:02:18 EDT 2002


Max M wrote:

> Michael Hudson wrote:
>> Syver Enstad <syver-en+usenet at online.no> writes:
> 
>>>Am I the only one in the entire world that thinks '\t'.join(mylist) is
>>>elegant and natural?
>> 
>> No.
> 
> Well I would have found mylist.join('\t') more natural, but I can live
> with the other.

joiner.join(sequence) is better: sequence is accessed through the
iterator protocol (now formally, once informally) so you get the
ability to play well with join for ANY kind of sequence of strings
as long as you support iteration (no need to do any work just for
join!) -- AND you can override join specifically for peculiar
joining needs, e.g. (rough -- needs fully indexable nonempty
sequence -- bit more work to refine for any iterable:-):

class mixedJoiner:
    def __init__(self, primary, last):
        self.primary = primary
        self.last = last
    def join(self, sequence):
        return self.primary.join(sequence[:-1]
            ) + self.last + sequence[-1]
    
print mixedJoiner(', ', ' and ').join('wine women song'.split())


joiner.join(sequence) was an EXCELLENT design choice...


Alex




More information about the Python-list mailing list