On Thu, Jun 10, 2021 at 11:58 PM Ricky Teachey <[email protected]> wrote: > > Something I don't understand is whether there is anything about this proposed > feature that can't be accomplished with a simple function. > > IIUC, the proposal turns this: > > foo = "spam & eggs" > `Here, have some {foo}.` > > ...into something like this (I am making up a more readable repr): > > TemplateLiteral( > template = " Here, have some {foo}.", > tokens = (("Here, have some ", True), ("spam & eggs", False)), > ) > > What is it about this task that cannot be accomplished with a class and > function? Skeleton might look like: > > @dataclass > class TemplateLiteral: > template: str > tokens: Sequence(Tuple(str, bool)) = field(init = False) > > def templify(s: str) -> TemplateLiteral: > ... > > And use it like this: > > >>> templify("Here, have some {foo}.") > TemplateLiteral(template = " Here, have some {foo}.", tokens = (("Here, have > some ", True), ("spam & eggs", False))) > > > What is it about this task that requires it being handled at the language > level...? >
The same thing that stops it for f-strings: there's no way to fetch variables from context other than either passing all your locals or repeating all the names multiple times. This proposal is basically for a way to take an f-string-like construct and, instead of calling format() on each of the values and joining them together into a string, you do something else with it. Or from a language perspective, you package it all up and hand it to a custom function. So it's basically an f-string minus the final step - which is why PEP 501 described f-strings in terms of interpolated strings. ChrisA _______________________________________________ Python-ideas mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/EFF6UFLFSO2YAQVMCBPHGWXHFUIP7V44/ Code of Conduct: http://python.org/psf/codeofconduct/
