news.volyx.in

PEP 750 – Template Strings (peps.python.org)

437 points by grep_it · 478 days ago · 337 comments on HN

Article summary

PEP 750 introduces template strings to Python, a generalization of f-strings that allows for more flexible string processing. Template strings are defined using the 't' prefix and evaluate to a new type, Template, which provides access to the string and its interpolated values. This feature enables safety checks, web templating, and domain-specific languages. The Template type has attributes such as 'strings' and 'interpolations' that can be used to process the template.

Main themes

  • Template Strings
  • String Processing
  • Python Enhancement Proposals
  • Security
  • Web Templating
  • Domain-Specific Languages

What commenters say

  • The introduction of template strings is a game-changer for applications involving HTML, SQL, or other languages embedded in Python programs.
  • Template strings do not address the issue of deferred evaluation, which is a significant limitation.
  • The use of lambda functions can help delay the evaluation of interpolations in template strings.
  • Some commenters argue that the design of template strings feels backwards and that the name 'template strings' is a misnomer due to the lack of deferred evaluation.
  • Others believe that the feature is useful and can help with tasks such as HTML escaping and SQL parameterization.
  • The use of template strings is seen as a way to improve developer ergonomics and reduce the risk of injection attacks.
  • Some commenters think that the feature is not necessary and that existing workarounds, such as using the format method, are sufficient.