msg276971 - (view) |
Author: Steven D'Aprano (steven.daprano) *  |
Date: 2016-09-19 11:21 |
It is moderately common to want to join a sequence of substrings with a delimiter rather than a separator, e.g. when joining a sequence of lines into a single string, you usually want a trailing newline as well as newlines between the lines. E.g.: '\n'.join(['first', 'second', 'third']) returns 'first\nsecond\nthird' but we usually want a trailing newline as well, but only if the iterable being joined is not empty. If there are no substrings, we don't want to append the delimiter. Currently the most obvious way to do this is to use a temporary variable: lines = '\n'.join(substrings) if lines: lines += '\n' process(lines) I propose adding a keyword-only argument to str.join(), "suffix", to specify an optional trailing substring added only if the iterable is non-empty. To join lines as above, you would write: process('\n'.join(substrings, suffix='\n')) eliminating the unnecessary temporary variable. Here's a proof of concept: def join(iterable, sep, *, suffix=None): s = sep.join(iterable) if s and suffix is not None: s += suffix return s |
|
|
msg276973 - (view) |
Author: Mark Dickinson (mark.dickinson) *  |
Date: 2016-09-19 11:51 |
> Currently the most obvious way to do this is to use a temporary variable: Or more simply: lines = ''.join(substring + '\n' for substring in substrings) |
|
|
msg276974 - (view) |
Author: Steve Holden (holdenweb) *  |
Date: 2016-09-19 12:02 |
If you are going to add such a keyword argument, wouldn't it make sense to maintain compatibility with print, and use end=terminator? |
|
|
msg276975 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2016-09-19 12:08 |
I'm -1. The str interface is already overburdened. This is very special case and there are several ways to do this (Mark's one is the most obvious to me). |
|
|
msg276985 - (view) |
Author: Barry A. Warsaw (barry) *  |
Date: 2016-09-19 20:58 |
I'm -1 also, mostly on the grounds that it's not (IME) a very common need and it does clutter up the API. |
|
|
msg276991 - (view) |
Author: Steven D'Aprano (steven.daprano) *  |
Date: 2016-09-19 23:22 |
> lines = ''.join(substring + '\n' for substring in substrings) Huh. There were three of us looking at this at work yesterday, and none of us thought of that. |
|
|
msg276993 - (view) |
Author: STINNER Victor (vstinner) *  |
Date: 2016-09-19 23:28 |
> '\n'.join(['first', 'second', 'third']) Hum, the workaround is simple: lines = ['first', 'second', 'third'] lines.append('') assert '\n'.join(lines) == 'first\nsecond\nthird\n' |
|
|
msg277005 - (view) |
Author: Raymond Hettinger (rhettinger) *  |
Date: 2016-09-20 04:54 |
> There were three of us looking at this at work yesterday, > and none of us thought of that. How about adding an example to the docs and calling it a day. |
|
|
msg277196 - (view) |
Author: Raymond Hettinger (rhettinger) *  |
Date: 2016-09-22 06:25 |
Looking again at the comments for the other respondents, I think this should just be closed. It doesn't make sense to disturb a long standing API or the break the join/split symmetry. |
|
|
msg277218 - (view) |
Author: Steven D'Aprano (steven.daprano) *  |
Date: 2016-09-22 11:17 |
> Looking again at the comments for the other respondents, I think this > should just be closed. It doesn't make sense to disturb a long > standing API or the break the join/split symmetry. For what it's worth, in hindsight I agree. I'm a little embarassed that I missed such a simple solution. Sorry for the noise! |
|
|