(评论)
(comments)

原始链接: https://news.ycombinator.com/item?id=43647716

PEP 750 提案在 Python 中引入模板字符串(t-字符串)已被接受,这在 Hacker News 社区引发了热议。用户称赞其能够延迟求值,解决了 f-字符串需要立即插值的限制。这允许创建模板而无需预先定义变量。 用户们也赞扬了 t-字符串处理自动 HTML 转义和 SQL 参数化的潜力,这类似于 JavaScript 的标签模板字面量。一些用户希望有一个内置的渲染函数,以避免冗余的实现。 一位用户预测 lint 工具将更倾向于 t-字符串,而另一位用户则强调了其改进开发者体验 (DX) 的潜力,尤其是在 HTML 模板化可能很繁琐的 Web 开发中。t-字符串可能使 Python 与 HTML 和 SQL 等语言更无缝地集成,通过降低字符串连接的吸引力来减少注入漏洞。虽然 Jinja 是一个流行的模板引擎,但 t-字符串有望提供更集成的 Python+HTML 工作流程。

相关文章
  • (评论) 2024-07-31
  • (评论) 2025-03-14
  • (评论) 2024-09-19
  • 安全的Shell字符串插值 2025-03-22
  • (评论) 2025-03-17

  • 原文
    Hacker News new | past | comments | ask | show | jobs | submit login
    PEP 750 – Template Strings (t-strings) have been accepted (python.org)
    76 points by grep_it 32 minutes ago | hide | past | favorite | 8 comments










    Most excellent! I love f-strings and replaced all the various other string interpolation instances in my code with them, but they have the significant issue that you can't defer evaluating them. For instance, you can write:

      >>> template = 'Hello, {name}'
      >>> template.format(name='Bob')
      'Hello, Bob'
    
    Until this, there wasn't a way to use f-strings formatting without interpolating the results at that moment:

      >>> template = f'Hello, {name}'
      Traceback (most recent call last):
        File "", line 1, in 
          template = f'Hello, {name}'
                               ^^^^
      NameError: name 'name' is not defined
    
    It was annoying being able to use f-strings almost everywhere, but str.format in enough odd corners that you have to put up with it.


    I'm excited about this. I really like how JavaScript's tagged template literals https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... can help handle things like automatic HTML escaping or SQL parameterization, it looks like these will bring the same capability to Python.


    This is super exciting but I wish they added a function to render the template to a string rather then having everyone write their own version of basic rendering.


    can't wait to have my linter tell me I should be using t-strings instead of f-strings. apologies for not being patient enough to read through this to find it, but I hope they can remove f-strings:

    > There should be one-- and preferably only one --obvious way to do it.



    This could be a game-changer for applications that involve lots of HTML, SQL, or other languages that tend to be embedded within Python programs. Frankly, HTML templating is the worst part of Python webdev DX for me.

    Excited to see what libraries and tooling comes out of this.



    These developer ergonomics reduce injection 90% in my experience.

    When concatenating strings is the harder approach, it is really beautiful.



    Whats wrong with jinja?


    Jinja et al. are fine. But they still forces me to choose Python OR HTML. I have to jump between files and LSPs. t-strings pave the way for true Python+HTML seamless DX.






    Join us for AI Startup School this June 16-17 in San Francisco!


    Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact



    Search:
    联系我们 contact @ memedata.com