[issue28205] Add optional suffix to str.join

2016-09-22 Thread Steven D'Aprano

Steven D'Aprano added the comment:

> 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!

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28205] Add optional suffix to str.join

2016-09-21 Thread Serhiy Storchaka

Changes by Serhiy Storchaka :


--
stage:  -> resolved

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28205] Add optional suffix to str.join

2016-09-21 Thread Raymond Hettinger

Raymond Hettinger added the comment:

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.

--
resolution:  -> rejected
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28205] Add optional suffix to str.join

2016-09-21 Thread Raymond Hettinger

Changes by Raymond Hettinger :


--
assignee:  -> rhettinger
components: +Documentation
priority: normal -> low

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28205] Add optional suffix to str.join

2016-09-19 Thread Raymond Hettinger

Raymond Hettinger added the comment:

> 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.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28205] Add optional suffix to str.join

2016-09-19 Thread Raymond Hettinger

Changes by Raymond Hettinger :


--
Removed message: http://bugs.python.org/msg277004

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28205] Add optional suffix to str.join

2016-09-19 Thread Raymond Hettinger

Raymond Hettinger added the comment:

-1 for this API expansion.  It would be better to have a separate method than 
to change the root notion of what join() is all about.

--
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28205] Add optional suffix to str.join

2016-09-19 Thread STINNER Victor

STINNER Victor added the comment:

> '\n'.join(['first', 'second', 'third'])

Hum, the workaround is simple:

lines = ['first', 'second', 'third']
lines.append('')
assert '\n'.join(lines) == 'first\nsecond\nthird\n'

--
nosy: +haypo

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28205] Add optional suffix to str.join

2016-09-19 Thread Steven D'Aprano

Steven D'Aprano added the comment:

> 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.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28205] Add optional suffix to str.join

2016-09-19 Thread Barry A. Warsaw

Barry A. Warsaw added the comment:

I'm -1 also, mostly on the grounds that it's not (IME) a very common need and 
it does clutter up the API.

--
nosy: +barry

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28205] Add optional suffix to str.join

2016-09-19 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

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).

--
nosy: +serhiy.storchaka

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28205] Add optional suffix to str.join

2016-09-19 Thread Steve Holden

Steve Holden added the comment:

If you are going to add such a keyword argument, wouldn't it make sense to 
maintain compatibility with print, and use end=terminator?

--
nosy: +holdenweb

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28205] Add optional suffix to str.join

2016-09-19 Thread Mark Dickinson

Mark Dickinson added the comment:

> 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)

--
nosy: +mark.dickinson

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28205] Add optional suffix to str.join

2016-09-19 Thread Steven D'Aprano

New submission from Steven D'Aprano:

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

--
messages: 276971
nosy: steven.daprano
priority: normal
severity: normal
status: open
title: Add optional suffix to str.join
type: enhancement
versions: Python 3.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com