[Python-checkins] CVS: python/dist/src/Lib string.py,1.46,1.47
Fredrik Lundh
effbot at telia.com
Tue Feb 22 04:19:32 EST 2000
Greg Ewing <greg at cosc.canterbury.ac.nz> wrote:
> Whatever you do, don't stuff it onto some arbitrary
> type just for the sake of making it a method.
excuse me? it's not an "arbitrary" type -- it's the
type of the separator string. makes perfect sense,
once you've learned to think like Tim...
> the world will not end if there are some non-method
> functions left in Python 1.6...
do you mean something like this?
def join(sequence, sep=" "):
if not sequence:
return sep[:0]
res = sequence[0][:0]
for w in sequence:
res = res + (sep + w)
return res[len(sep):]
or maybe, to get decent performance out of it:
def join(sequence, separator=" "):
if type(separator) is StringType:
... do it this way ...
elif type(separator) is UnicodeType:
... or do it that way ...
# remember to add more code here
# if we add more string types
else:
raise TypeError
or maybe, a bit more object oriented:
def join(sequence, separator=" "):
return separator.join(sequence)
now, the last one wasn't too bad, was it?
(if you liked that, have you looked in the
CVS repository lately? ;-)
</F>
More information about the Python-list
mailing list