Currently you can write

    term.join(xs) + term

if you want 1, 1, 2, 3, ... terminators when xs has 0, 1, 2, 3, ...
elements, and

     term.join([*xs, ''])  # or b''

if you want 0, 1, 2, 3, ... terminators, neither of which is prohibitively
annoying. What I don't like about the status quo is that I'm never sure
that the people who wrote "sep.join(xs) + sep" really want the separator
when xs is empty. Unless it's obvious from surrounding code that xs can't
be empty, I always worry it's a bug. In your PIL example,

        arr = []
        for elt in (description, cpright):
            if elt:
                arr.append(elt)
        return "\r\n\r\n".join(arr) + "\r\n\r\n"

do they really want four newlines when the description and copyright are
both empty? I suspect not but I don't know. There's no clarifying comment.
In email.contentmanager they call splitlines on some text, then rejoin it
with '\n'.join(lines) + '\n'. It looks like the input can be empty since
there is some special-case code for that. Do they know that they're
increasing the number of newlines in that case? There's no clarifying
comment.

As for term.join([*xs, '']), while it seems less likely to be a bug, it's
not very natural. You aren't adding an extra blank thing at the end, you're
just terminating the things you already had.

So I think it would be nice to have a way to say explicitly and concisely
what you want to happen with an empty list. I suppose this idea will fail
for the usual reason (no good syntax), but here's an attempt:

    term.joinlines(xs)            in place of   term.join([*xs, ''])
    term.joinlines(xs) or term    in place of   term.join(xs) + term

The second one is less concise than before, but it doesn't give me that
uneasy feeling.

Adding a separator before the first element doesn't seem important enough
to me to justify the complexity of adding an option for it.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/ZZ4MNCP2AFWKLAAT4QZFOM7LBWNPES7B/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to