TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

PEP 750: Tag Strings for Writing Domain-Specific Languages

96 点作者 lumpa9 个月前

24 条评论

mrweasel9 个月前
My issue with this that is will eventually sneak into libraries and the users of that library would be expected to use these tag strings all over the place to utilize the library. This prevents people from having a uniform coding style and make code harder to read.<p>The concern isn&#x27;t having features that will make it easier to write DSLs, my problem is that people will misuse it in regular Python projects.<p>I know that one of the authors are Guido, but I&#x27;m not buying the motivation. Jinja2 and Django template are pretty much just using Python, it&#x27;s not really much of an issue, and I don&#x27;t believe that business logic should exist in your templates anyway. As for the SQL argument, it will still be possible for people to mess it up, even with Tag Strings, unless you completely remove all legacy code. The issue here isn&#x27;t that the existing facilities aren&#x27;t good enough, it&#x27;s that many developers aren&#x27;t aware of the concepts, like prepared statements. If developers aren&#x27;t reading the docs to learn about prepared statements, why would they do so for some DSL developed using tag strings?<p>Obviously Guido is a better developer than me any day, so I might be completely wrong, but this doesn&#x27;t feel right. I&#x27;ve seen tools developed to avoid just doing the proper training, and the result is always worse.
评论 #41211306 未加载
评论 #41217466 未加载
评论 #41211258 未加载
评论 #41211377 未加载
dmart9 个月前
I have to admit that at first glance I don’t like this. These seem to be essentially normal str -&gt; Any functions, with some naming limitations due to the existing string prefixes special-cased in the language. I don’t feel like adding this additional complexity is worth being able to save two parentheses per function call.
评论 #41211119 未加载
评论 #41211097 未加载
评论 #41210977 未加载
ianbicking9 个月前
I LOVE tagged templates in JavaScript.<p>But in Python I could also imagine YET ANOTHER constant prefix, like t&quot;&quot;, that returns a &quot;template&quot; object of some sort, and then you could do html(t&quot;&quot;) or whatever. That is, it would be just like f&quot;&quot; but return an object and not a string so you could get at the underlying values. Like in JavaScript the ability to see the original backslash escaping would be nice, and as an improvement over JavaScript the ability to see the expressions as text would also be nice.<p>But the deferred evaluation seems iffy to me. Like I can see all the cool things one might do with it, but it also means you can&#x27;t understand the evaluation without understanding the tag implementation.<p>Also I don&#x27;t think deferred evaluation is enough to make this an opportunity for a &quot;full&quot; DSL. Something like a for loop requires introducing new variables local to the template&#x2F;language, and that&#x27;s really beyond what this should be, or what deferred evaluation would allow.
评论 #41217797 未加载
TwentyPosts9 个月前
This like a bad idea on the first glance? Maybe I don&#x27;t get the whole pitch here?<p>It just doesn&#x27;t seem worth it to define a whole new thing just to abstract over a format() function call. The laziness might be interesting, but I feel like &quot;lazy strings&quot; might be all that&#x27;s needed here. Laziness and validation (or custom string formatting logic) are separate concerns and should be separated.
评论 #41211250 未加载
DataDive9 个月前
Excellent idea, I don&#x27;t get the criticism,<p>If a syntax such as f&quot;{variable}&quot; is already a feature - and turned out to be a popular one - why shouldn&#x27;t we be able to add our own custom &quot;f&quot;s? Because that is what this is about. It might make generating output even simpler.<p>I applaud the idea and am pleased to see that Python keeps innovating!
评论 #41213444 未加载
Hamuko9 个月前
I hate the idea of reusing the existing string&#x2F;bytes prefixes for something that is completely different. How is someone expected to know that br&quot;&quot; is inherent Python syntax and my&quot;&quot; is essentially an user-defined function? And the only way to ever add a new prefix into the language (like f&quot;&quot; was added quite recently) is to wait until Python 4, at which point we&#x27;ll need 3to4 to automatically rename all of your old tag strings that are now conflicting and people will bitch about how badly major Python upgrades suck.
ziml779 个月前
It seems the purpose of this proposal is to have a way to essentially have custom string interpolation. I don&#x27;t think that&#x27;s necessarily a bad idea on its own, but this syntax feels out of place to me.<p>Instead, why not add a single new string prefix, like &quot;l&quot; for &quot;lazy&quot;? So, f&quot;hello {name}&quot; would immediately format it while l&quot;hello {name}&quot; would produce an object which contains a template and the captured variables. Then their example would be called like: greet(l&quot;hello {name}&quot;).
treyd9 个月前
I can&#x27;t help but believe that this is introducing <i>more</i> spooky action at a distance and is bound to be abused. Is it really more usable this way? Do they have any concrete and practical examples where this improves readability?
tofflos9 个月前
I would have loved to see Java introduce something similar to the IntelliJ @Language-annotation in the standard library but maybe they&#x27;ll figure out the sweet spot in a future String Templating JEP.<p><pre><code> @Language(&quot;application&#x2F;sql&quot;) String query = &quot;SELECT 1&quot;; @Language(&quot;application&#x2F;graphql+json&quot;) String query = &quot;&quot;&quot; query HeroNameAndFriends { hero { name friends { name } } } &quot;&quot;&quot;;</code></pre>
评论 #41214413 未加载
formerly_proven9 个月前
Yikes. Don&#x27;t get me wrong, I totally understand the reasoning why this would be useful (though I <i>violently disagree</i> with the idea of deferring the evaluation of the contained expressions), but it&#x27;s also so very kitchensinky and adds so little over just calling a function (which doesn&#x27;t require a 20-page explainer, as everyone already knows how function calls work). It also promotes using what looks like string interpolation (and what might be string interpolation, you can&#x27;t tell at the &quot;call site&quot;) for things which we know string interpolation is the wrong tool. The API also seems really, I dunno, weird to me. The string is split around interpolations and verbatim portions result in one argument, which is &quot;string-like&quot;, while interpolations become four-tuple-like (one of which is a lambda, which you call to perform the deferred interpolation). This seems really awkward to me for building stuff like the suggested use cases of XML&#x2F;HTML or SQL templating.<p>Also the scoping rules of this are a special case which doesn&#x27;t appear in regular Python code so far: &quot;The use of annotation scope means it’s not possible to fully desugar interpolations into Python code. Instead it’s as if one is writing interpolation_lambda: tag, not lambda: tag, where a hypothetical interpolation_lambda keyword variant uses annotation scope instead of the standard function scope.&quot; -- i.e. it&#x27;s &quot;as if you wrapped all interpolation expressions in a lambda: &lt;expr&gt;, <i>except it uses different scoping rules</i>&quot;.
评论 #41211329 未加载
cr125rider9 个月前
This seems very unpythonic in the way that it breaks the one best way to do things adage. It’s syntax sugar for a function call. Just keep it a function call if needed.
Spivak9 个月前
The <i>much</i> bigger feature here is buried under the DSL stuff. Python is effectively implementing a method of lazy evaluation of function arguments! I thought I would never see the day! It&#x27;s crazy that if this PEP is accepted, functions in Python will actually be a special case of f-strings.<p>I hope they eventually grant this power to regular functions because otherwise I know folks will end up myfunc&quot;{arg1},{arg2}&quot; to get that feature.
nope969 个月前
Does this mean I can write a print function and be able to print&quot;hello world&quot; without parentheses again, like in python 2.x ?
pansa29 个月前
Off-topic, but when did Python become so... verbose? From the PEP:<p><pre><code> def greet(*args: Decoded | Interpolation) -&gt; str: result = [] for arg in args: match arg: case Decoded() as decoded: result.append(decoded) case Interpolation() as interpolation: value = interpolation.getvalue() result.append(value.upper()) return f&quot;{&#x27;&#x27;.join(result)}!&quot; </code></pre> Isn&#x27;t that just this?<p><pre><code> def greet(*args): def convert(arg): return arg.getvalue().upper() if hasattr(arg, getvalue) else arg return &#x27;&#x27;.join(convert(arg) for arg in args) + &#x27;!&#x27;</code></pre>
评论 #41213908 未加载
orbisvicis9 个月前
This seems similar to my protostrings library [1] which I wrote years ago and mostly forgot about till now.<p>1. <a href="https:&#x2F;&#x2F;protostrings.readthedocs.io&#x2F;en&#x2F;latest&#x2F;index.xhtml" rel="nofollow">https:&#x2F;&#x2F;protostrings.readthedocs.io&#x2F;en&#x2F;latest&#x2F;index.xhtml</a><p>iirc I wanted to encode state in a recursive-descent parser without additional complexity to the parser.<p>Similar in purpose not design; protostrings provides lazy and context-sensitive strings from the bottom up rather than this top-down template-style proposal, which I feel addresses most of the concerns here.
spankalee9 个月前
I tried skimming the PEP while I could, but it seems like this might be missing a couple of the features that make JS tagged template literals work so well:<p>- tags get a strings array that&#x27;s referentially stable across invocations. This can function as a cache key to cache strings parsing work. - tags can return any kind of value, not just a string. Often you need to give structured data to another cooperating API.<p>Deferred evaluation of expressions is very cool, and would be really useful for reactive use-cases, assuming they can be evaluated multiple times.
评论 #41272352 未加载
评论 #41211587 未加载
jtwaleson9 个月前
For people adding insightful critique on the PEP on HN (I saw some on this thread already), please ensure your opinion is represented in the PEP thread itself too.
评论 #41211363 未加载
评论 #41212049 未加载
samatman9 个月前
I think this will turn out well. Julia has had this forever as string macros, and it has worked out rather nicely, features like `r&quot;\d+&quot;` for regex, and `raw&quot;strings&quot;` are just string macros. The set of all useful custom literal strings isn&#x27;t bounded, so a lightweight mechanism to define them and make use of the results is a good thing.<p>Another kitchen sink to add to Python&#x27;s world-class kitchen sink collection.
kbd9 个月前
At least in the spirit of &quot;the language shouldn&#x27;t be able to define things the user can&#x27;t&quot; (see: Java string concatenation) this seems like a good change.
Too9 个月前
Looks good. Would have been nice if they included a way to express type checking of the format_spec. That’s going to be an unnecessary source of runtime errors.
评论 #41211334 未加载
zoogeny9 个月前
I&#x27;ve seen this feature used responsibly and to good effect in a few TypeScript projects so I understand why it would be desirable in Python.
评论 #41217286 未加载
agumonkey9 个月前
Seems like many languages are allowing compile time interception (zig, es, now python)
Groxx9 个月前
... is this any different than a function like this:<p><pre><code> greet(&quot;hello {world}&quot;) </code></pre> which walks the call stack to find the variables, and uses them as locals &#x2F; arguments?<p>If so: why not just do that? I would expect the performance to be kinda terrible compared to a language-intrinsic, but this hardly seems like a thing worth truly optimizing. And if it&#x27;s too costly at runtime, someone can implement a parse-time AST rewriter, like many Python things already do. Heck, that example&#x27;s `assert` is probably using pytest (because everyone uses pytest) and it&#x27;s doing exactly this already, and it isn&#x27;t a <i>language feature</i>, it&#x27;s just a normal library using normal Python features.
评论 #41272385 未加载
behnamoh9 个月前
I want this in Python: <a href="https:&#x2F;&#x2F;codecodeship.com&#x2F;blog&#x2F;2024-06-03-curl_req" rel="nofollow">https:&#x2F;&#x2F;codecodeship.com&#x2F;blog&#x2F;2024-06-03-curl_req</a><p>From the article: &quot;&quot;&quot;<p><pre><code> ~CURL[curl https:&#x2F;&#x2F;catfact.ninja&#x2F;fact] |&gt; Req.request!() </code></pre> This is actual code; you can run this. It will convert the curl command into a Req request and you will get a response back. This is really great, because we have been able to increase the expressiveness of the language. &quot;&quot;&quot;
评论 #41214742 未加载