
Am Fr., 11. Juni 2021 um 14:51 Uhr schrieb Ricky Teachey <ricky@teachey.org
:
I think this idea is promising but instead of doing it by adding new syntax and a totally different object, why not attach a __templates__ dunder member to every string but only OPTIONALLY populate it when a string is formatted?
I am very happy that you think this is promising. Changing the interface of every string in Python feels much too big for me. I don't dare to think about it. I have concerns that it could have negative impacts on parts which I am not aware of.
For every regular string it would just be None:
"".__template__
But if you create a string using an f string, you can provide a directive, as part of the format specification mini language, to store it:
s = f"{foo!t}"
...or a directive at the front of the string to store all the f string arguments:
s = ft"{foo}"
This would save the values marked for storage as a tuple of Template arguments in the __templates__ member for later. It may also be desirable for the Template object to store the name foo and the position in the string the foo object was formatted.
The "t" prefix makes sense. I would reduce it from ft'...' to t'...'. But it looks good. This is in the "alternatives": https://github.com/guettli/peps/blob/master/pep-9999.rst#alternative-ideas I am happy with backticks and/or t'...'. Backticks look a bit "cleaner" to my eyes, but I am fine with the "t" prefix, too.