On 2023-04-26 02:16, Joao S. O. Bueno wrote:
On Sat, Apr 22, 2023 at 10:06 AM Damian Cross <damnedboy92@gmail.com <mailto:damnedboy92@gmail.com>> wrote:
That would have the effect that every use of str.format for everyone would start producing partially-formatted strings if an argument is accidentally omitted instead of raising an error. Some people might not like that. _______________________________________________
If that is all there is for a downside, this is actually quite weak. You just changed my mind to +1 on the proposal.
Worst case scenario, one goes from one non-running program to a running program producing partially incorrect output. Any legacy code that was not working in the first place, is obviously, clearly, not critical for anyone, otherwise it would have been fixed already.
We can't stal all augmenting to language functionalities because "some is used with the fact writing incorrect code in this way used to produce an error before". Ultimately, by this logic, it would be impossible to add even any new keyword only parameters to any stdlib call, because "there might be some code out there using this parameter, and that used to raise an error"
The problem is that the resulting string might or might not be fully formatted, but you wouldn't be sure. The original post itself almost demonstrates the issue. I say "almost" because it has:
r"\mathjax{{color}}{{text}}".format(color="blue", text="Spanish")
which is '\\mathjax{color}{text}', not "\\mathjax{blue}{Spanish}", because "{{" and "}}" are literals. After correction:
pfr = r"\mathjax{{{color}}}{{{text}}}".format(color="blue")
So, pfr == r"\mathjax{blue}{{{text}}}". That looks like a format string with 2 placeholders, "{blue}" and "{text}". And what happens if the partially-formatted string isn't a valid format string? So I'm -1 on the idea. You'd be better off creating a new Format class that parses a format string and lets you insert values:
fmt = Format(r"\mathjax{{{color}}}{{{text}}}") fmt Format('\\mathjax{{{color}}}{{{text}}}')
(Calling it "Format" has the disadvantage that it's too close to the build-in function "format".) Placeholders can be positional or named, which is like the arguments of function calls, so maybe it's a callable:
fmt = fmt(color="blue") fmt Format('\\mathjax{{{color}}}{{{text}}}', color='blue') fmt = fmt(text="Spanish") fmt Format('\\mathjax{{{color}}}{{{text}}}', color='blue', text='Spanish') str(fmt) '\\mathjax{blue}{Spanish}' print(fmt) \mathjax{blue}{Spanish}
The advantages of leaving it as a format each time are 1) it can ignore unneeded values and 2) it consistently returns an instance of the same type.