On 2023-04-26 11:03 a.m., MRAB wrote:
On 2023-04-26 02:16, Joao S. O. Bueno wrote:
On Sat, Apr 22, 2023 at 10:06 AM Damian Cross <damnedbo...@gmail.com
<mailto:damnedbo...@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}".
It's probably worse than that. I would suspect/expect pfr to be
r"\mathjax{blue}{text}"; I see no reason why the brace literals
surrounding {text} would be left unescaped and the other ones escaped.
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.
Doesn't that essentially already exist as `string.Template` (albeit with
a different though configurable identifier substitution language).
>>> import string
>>>
string.Template(r'\mathjax{$color}{$text}').safe_substitute(color='blue')
r'\mathjax{blue}{$text}'
_______________________________________________
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/KKH7QHEMQDWH53SV7PBUW4N2IH63YV6Z/
Code of Conduct: http://python.org/psf/codeofconduct/