7 ms·
Slowly but surely... PEP 750 (still a proposal) adds "t-strings" Python. They're a simple extension of f-strings that give developers access to the static and
by davepeck 2y ago
Slowly but surely...
PEP 750 (still a proposal) adds "t-strings" Python. They're a simple extension of f-strings that give developers access to the static and interpolated parts of the string before they get combined into a final string. They're Python's answer to JavaScript template literals.
This allows writing code like:
user_name = current_user.user_name # something user-supplied and potentially unsafe
foo: HTMLElement = html(t"<div>{user_name}</div>") # wouldn't be safe with f-strings
where `html` is a normal function that takes a `Template` as an instance, and can return whatever it likes -- say, an `HTMLElement` rather than a `str`.
If PEP 750 does get accepted, eventually one hopes the community will introduce formatting, type checking, and LSP smarts for common kinds of t-strings, like HTML and maybe SQL, etc.
- pphysch 2y agoGreat! Hope the proposal gets support.