On Sat, 29 Apr 2023 at 23:36, MRAB <python@mrabarnett.plus.com> wrote:
On 2023-04-29 08:31, Matsuoka Takuo wrote:
On Sat, 29 Apr 2023 at 00:52, MRAB <python@mrabarnett.plus.com> wrote:
What happens if you do '{open}...{close}'.partial_format(open='{close}'? You get '{close}...{close}', and you're going to have a problem using that as a format string and replacing only the second '{close}'.
Or how about '{open}...{close}'.partial_format(open='{')? You get '{...{close}'. Try using that as a format string!
That's why I think that the result of a partial format should be an instance of a new class and not a string.
That's a quite subtle point. I think '{open}...{close}'.partial_format(open='{close}') could be the string
'{open}...{close}'.format(open='{{close}}', close='{close}')
and '{open}...{close}'.partial_format(open='{') could be '{open}...{close}'.format(open='{{', close='{close}')
You might not know what the replacements are until runtime, so:
'{open}...{close}'.partial_format(open=opening_item)
A good point, but that's not a problem. Escape of braces can and must be done after the argument is formatted. I hope the expression '{open}...{close}'.format( open=re.sub('([{}])', r'\1\1', f'{opening_item}'), close='{close}' ) looks better. (I'm only describing the imaginable result here, and not the exact procedure to get to it. I can only say there is at least one way to get to it.)