I’m constantly wrapping my str.join() arguments in a list, e.g.
'.'.join([str_one, str_two])
The extra list wrapper always seems superfluous to me. I’d like to do…
'.'.join(str_one, str_two, str_three, ...)
… or if I have a list …
'.'.join(*list_of_strings)
Yes I’m a minimalist, yes I’m picky, but mostly I’m just curious about the history here, or whether I’m missing something. Maybe there was a time before splats?
Edit:
I’d just like to note that max() handles both versions:
max(iterable[, key])
max(arg1, arg2, *args[, key])
For short lists this won’t matter and it costs you exactly 2 characters to type. But the most common use-case (I think) for
str.join()is following:where input_data can have thousand of entries and you just want to generate the output string efficiently.
If join accepted variable arguments instead of an iterable, this would have to be spelled as:
which would create a (possibly long) tuple, just to pass it as
*args.So that’s 2 characters in a short case vs. being wasteful in large data case.
History note
(Second Edit: based on HISTORY file which contains missing release from all releases. Thanks Don.)
The
*argsin function definitions were added in Python long time ago:A proper way to pass a list to such functions was using a built-in function
apply(callable, sequence). (Note, this doesn’t mention**kwargswhich can be first seen in docs for version 1.4).The ability to call a function with
*syntax is first mentioned in release notes for 1.6:But it’s missing from grammar docs until version 2.2.
Before 2.0
str.join()did not even exists and you had to dofrom string import join.