[Python-3000] Automatically invoking str() in str.join()
Nick Coghlan
ncoghlan at gmail.com
Mon May 1 13:10:32 CEST 2006
Thomas Wouters wrote:
> It doesn't matter. Either case is confusing, one way or another, as Tim
> has already argued. Changing it would be a big mistake. If you want me
> to come with more comprehensive arguments (other than what Tim already
> covered), please come with more comprehensive arguments *for* the
> change, too.
What if the join() builtin had a similar signature to min() & max(), and the
separator was a keyword only argument? Something like:
def join(*args, **kwds):
# Need a mutable seq to handle type conversions
if len(args) == 1:
seq = list(args[0]) # Single argument handled as iterator
else:
seq = list(args) # Multiple arguments also accepted
# Check for a custom separator (keyword argument only)
sep = kwds.pop('sep', '')
assert not kwds
assert isinstance(sep, basestring)
# Check if we want unicode or 8-bit strings
sep_is_unicode = isinstance(sep, unicode)
use_unicode = sep_is_unicode or any(isinstance(x, unicode) for x in seq)
# Convert all items to desired type
if use_unicode:
desired = unicode
if not sep_is_unicode:
sep = unicode(sep)
else:
desired = str
for i, item in enumerate(seq):
if not isinstance(item, desired):
seq[i] = desired(item)
# Perform the join operation
return sep.join(seq)
(as with min() and max(), a single argument which is a non-sequence doesn't
make sense, as it would simply mean that join(x) == x).
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
More information about the Python-3000
mailing list