
On Mon, Mar 6, 2023 at 12:51 AM David Mertz, Ph.D. <david.mertz@gmail.com> wrote:
Is it really that much longer to write `f"{s1} {s2}"` when you want that?
As for being that much longer: yes it is. The more important factor is, I think, the increase in complexity + readabiity for default strings is worth it in this case. One nice thing to think about might be how to make string subclassing to be more useful - so this kind of thing could be done for whoever needs it in one more idiomatic project. The drawback is how cumbersome it is to instantiate a string subclass compared to a string literal. (I just got an idea, but it would be too offtopic here - if I think it is worth, I will post it in a new thread later)
Maybe a couple characters more total, but once you are in an f-string, you can also do a zillion other things at the same time.
On Sun, Mar 5, 2023 at 10:42 PM Rob Cliffe via Python-ideas < python-ideas@python.org> wrote:
Tl;dr: Join strings together with exactly one space between non-blank text where they join.
I propose a meaning for s1 & s2 where s1 and s2 are strings. Namely, that it should be equivalent to s1.rstrip() + (' ' if (s1.strip() and s2.strip()) else '') + s2.lstrip() Informally, this will join the two strings together with exactly one space between the last non-blank text in s1 and the first non-blank text in s2. Example: " bar " & " foo " == " bar foo "
This operator is associative, so there is no ambiguity in expressions such as s1 & s2 & s3 There *is* a possible ambiguity in expressions such as s1 & s2 + s3 where the relative precedence of `&` and `+` matters when s2 consists solely of white space. E.g. " A " & " " + " C" would evaluate to " A C" not to " A C" because `+` has a higher precedence than '&'.
Utility: In decades of experience with another language which had such an operator (spelt differently) I have frequently found it useful for constructing human-readable output (e.g. log output, debug/error messages, on-screen labels).
Cognitive burden: This would of course be one more thing to learn. But I suggest that it is fairly intuitive that s1 + s2 s1 & s2 both suggest that two strings are being combined in some way.
Bars to overcome: This change would require no fundamental change to Python; just adding an `__and__ function to the str class.
Backward compatibility: Given that `str1 & str2` currently raises TypeError, this change would be close to 100% backward-compatible.
Alternative meanings: As far as I know nobody has ever suggested an alternative meaning for `&` on strings.
Bikeshedding: (1) I don't think it is important for the utility of this change whether `&` strips off all whitespace, or just spaces. I think it is better if it strips off all whitespace, so that it can be understood as working similarly to strip(). (2) The definition could be simplified to s1.rstrip() + ' ' + s2.lstrip() (leave an initial/final space when one string is whitespace only). Again the operator would be associative. Again I don't think this is important.
Rob Cliffe _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/66XP7G... Code of Conduct: http://python.org/psf/codeofconduct/
-- The dead increasingly dominate and strangle both the living and the not-yet born. Vampiric capital and undead corporate persons abuse the lives and control the thoughts of homo faber. Ideas, once born, become abortifacients against new conceptions. _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/M7H3VZ... Code of Conduct: http://python.org/psf/codeofconduct/