
Am Fr., 11. Juni 2021 um 00:10 Uhr schrieb Christopher Barker < pythonchb@gmail.com>:
There may well be use cases for this, but one thing struck me. From the PEP:
"Template Literals provide an easy way to access the local and global variables (like f-strings), so that passing a dictionary to the Template is not necessary."
This seems to be crossing the line between "data" and "code" -- that's a line that can get pretty fuzzy in Python, but it's still a distinction I find helpful to think about.
f-strings provide an easy way to build strings with stuff stored in code: local variables. This makes them very helpful for things like building Exception messages and the like, or showing the results of computation.
But for the most part, populating a template is likely to be done from data, rather than code -- results of a database query, or what have you. So this kind of template building is usually very well suited to passing a dictionary around, rather than using the local namespace.
If you don't create HTML with Python daily, then you might not feel the pain. If you create many HTML strings daily, then you will be typing `foo=foo, bar=bar` (to pass the variables into the template) over and over again. I would reduce cognitive load if you could avoid reading/writing `foo=foo, bar=bar`. My goal is to have a super reduced syntax, so that developers need only a few characters to create save (properly escaped) html with Python. In the past people created whole HTML pages. There it made sense to use a template language. But if you use FrOW, then you will create many small methods returning small fragments, and then this small extra work of passing in variables gets .... (please insert your favorite negative adjective). Back your point "data vs code". The new class types.TemplateLiteral is pure data. It is up to the consumer to process the data. Of course I am biased, since my main concern is creating HTML. But I guess there are several other use-cases where a TemplateLiteral could be used.
Even if you are using Python objects to model your data (e.g. dataclasses and the like) -- you still have: a) one object to pass in to your template builder and/or b) an easy way to make a dict out of the object to pass into a template.
I could be missing something, but I just don't see the benefits of having f-string like access to local variables in this context.
-CHB
- Desktop GUI and Web Development - wxPython, numpy, scipy, Cython