Having an operand blank or all whitespace is not a common use case for join, nor would it be for my proposed '&' operator.  I wanted to present a clear definition, and however I chose it, those corner cases would be non-obvious. That said, I have now withdrawn this proposal due to objections by Bruce Leban and others.
Best wishes
Rob Cliffe

On 11/03/2023 17:13, Antoine Rozo wrote:
It's not that tricky to have the lstrip/rstrip behaviour with a join:
def space_join(*args):
    first, *middle, last = args
    return ' '.join((first.rstrip(), *(s.strip() for s in middle), last.lstrip()))

What is harder is to be sure that this would be the expected behaviour when using a `&` operator on strings. Why `'   a' & 'b'` would produce `'a b'` and `'   ' & 'b'` produce `' b'` for example?

Le sam. 11 mars 2023 à 10:04, Rob Cliffe via Python-ideas <python-ideas@python.org> a écrit :



    On 07/03/2023 09:54, Valentin Berlier wrote:
    > I'm -1 on this. You can easily make a helper that achieves the
    desired syntax. Presenting "human readable data" isn't just about
    collapsing spaces, and having your own helper means that you can
    adjust the formatting to your specific use case if needed (for
    example with a different separator).
    >
    >      from typing import Self
    >
    >      class StripJoin:
    >          def __init__(self, value: str = "") -> None:
    >              self.value = value
    >          def __and__(self, other: str) -> Self:
    >              other = other.strip()
    >              separator = bool(self.value and other) * " "
    >              return StripJoin(f"{self.value}{separator}{other}")
    >          def __str__(self) -> str:
    >              return self.value
    >
    >      j = StripJoin()
    >      print(j & "   foo  " & " bar   " & " something   ")
    >      # Output: "foo bar something"
    >
    > The example above is more efficient than a possible
    implementation directly on the str builtin as it doesn't strip the
    left side over and over. However it still incurs repeated
    allocations and encourages a pattern that performs badly in loops.
    With a lot of input you should probably accumulate the stripped 
    strings in a list and join them all at once.
    As Steven d'Aprano pointed out re a similar suggestion, this is
    not the
    same as my proposal, where
         " foo " & " bar " & " something "
    would evaluated to
         "   foo bar something   "
    Far from stripping the left side over and over, it doesn't strip
    it at
    all!  (Or the right side.)
    This is trickier to write using join.  And if the first or last
    string
    can be blank, or all whitespace, it is trickier still.
    As it is so easy to get these things wrong, perhaps having it
    built in
    is not such a terrible idea?😁
    Best wishes
    Rob Cliffe
    >
    > In any case I recommend reaching out for a library like Rich
    (https://github.com/Textualize/rich) if you care about formatting
    the output of your program nicely.
    > _______________________________________________
    > 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/A7RPR3FSBXEMRYAUXJVYYROCHVHL7DVP/
    > Code of Conduct: http://python.org/psf/codeofconduct/

    _______________________________________________
    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/J356SW4PG3KUDYFBLXAWDS723VCV3EVE/
    Code of Conduct: http://python.org/psf/codeofconduct/



--
Antoine Rozo
_______________________________________________
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/KYWCKX2NSOOFURGMEPITXMLUBADDSEL6/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to