On 09/03/2023 05:25, Bruce Leban wrote:

On Wed, Mar 8, 2023 at 4:34 PM Rob Cliffe via Python-ideas <python-ideas@python.org> wrote:

    It seems to me that it would be useful to be able to make the
    str.join()
    function put separators, not only between the items of its
    operand, but
    also optionally at the beginning or end.
    E.g. '|'.join(('Spam', 'Ham', 'Eggs')) returns
         'Spam|Ham|Eggs'
    but it might be useful to make it return one of
         '|Spam|Ham|Eggs'
         'Spam|Ham|Eggs|'
         '|Spam|Ham|Eggs|'


Compare these two lines:

    '\n'.join(lines) + '\n'
    '\n'.join(lines, atEnds=1)


The first is not only shorter, it's more clear what it's doing. I'd have to look up everytime to remember whether the value atEnds=1 is doing the right thing. It's just IMHO not valuable enough for the cost. Not every potential optimization is worth including in the core language or the stdlib (if this even is an optimization).

I don't think your comparison is fair.
If the second one were written
'\n'.join(lines, 1)
it would be shorter.  And if it were spelt
  '\n'.join(lines, 'e') # s for at Start, e for at End, b for Both
which I now think is preferable, it would still be shorter and you probably wouldn't need to look it up.
And when it comes to examples like
return policy.linesep.join(lines) + policy.linesep
        return policy.linesep.join(lines, 'e')
it would save even more characters and very likely be (more of) an optimisation.

Ben mentions being able to specify if a terminal separator is wanted when the list is empty.  That is an idea which I think is worth considering.  How about:
    'S' = always add a separator at start
    's' = add a separator at start unless the list is empty
    'E' = always add a separator at end
    'e' = add a separator at end unless the list is empty
with these combinations being allowed:
    'se, 'SE', 'Se', 'sE'
(the last two would have the same effect, but the difference in emphasis might clarify the author's thought). If nothing else, this would push authors into thinking about the empty list case, rather than being sloppy about it as Ben suspects in one or two cases.

Best wishes
Rob Cliffe
_______________________________________________
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/UXPYKBZL7Z2XQUH2DWXPT5A3QVCLUOAT/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to