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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Adding C-style for loops to Python (2022)

211 点作者 RojerGS超过 2 年前

25 条评论

traverseda超过 2 年前
Anyone looking to do this kind of thing, the macropy library makes it a fair bit easier: <a href="https:&#x2F;&#x2F;macropy3.readthedocs.io&#x2F;en&#x2F;latest&#x2F;" rel="nofollow">https:&#x2F;&#x2F;macropy3.readthedocs.io&#x2F;en&#x2F;latest&#x2F;</a><p>You really do need to be modifying the AST and not just applying a regex to the source code text. This can be a challenge if you want to modify python syntax as you first need to get an AST you can modify, personally I recommend starting with the LARK library and modifying the python syntax grammer it includes.<p><a href="https:&#x2F;&#x2F;lark-parser.readthedocs.io&#x2F;en&#x2F;latest&#x2F;examples&#x2F;advanced&#x2F;python_parser.html" rel="nofollow">https:&#x2F;&#x2F;lark-parser.readthedocs.io&#x2F;en&#x2F;latest&#x2F;examples&#x2F;advanc...</a><p>I use similar techniques to transpile openscad code into a python AST in my &quot;pySdfScad&quot; project, while still theoretically getting the benefits of fancy tracebacks and debugging and the like. Probably should have gone with a simple parser instead, but what can you do.<p>I think they should have stopped at the &quot;cursed way&quot; and not the &quot;truly cursed way&quot;, if they really wanted the syntax changes than having your own python parser like the LARK implementation I mention above is a must.
评论 #34651793 未加载
albertzeyer超过 2 年前
I don&#x27;t understand the reasoning why the transformation directly on the source code is better than on the AST level?<p>Because you can use coding and then it&#x27;s somewhat automatic?<p>But you can do sth similar on AST level, namely installing a meta path finder, i.e. an import module hook which does the AST transformation automatically (<a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;sys.html#sys.meta_path" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;sys.html#sys.meta_path</a>).<p>Then, there is also the new frame evaluation API (<a href="https:&#x2F;&#x2F;peps.python.org&#x2F;pep-0523&#x2F;" rel="nofollow">https:&#x2F;&#x2F;peps.python.org&#x2F;pep-0523&#x2F;</a>), which allows you to dynamically rewrite bytecode. This has been used by PyTorch 2.0 for torch.compile.
评论 #34647614 未加载
评论 #34646995 未加载
评论 #34647366 未加载
chriskw超过 2 年前
I did something similar to option 3 to make the builtin numeric types &quot;callable&quot;, since the dunder __call__ methods can&#x27;t be overwritten for builtins. For example, in regular arithmetic notation, something like 6(7+8) could be read as 6*(7+8), but trying to do this in Python gives you `SyntaxWarning: &#x27;int&#x27; object is not callable;`, since you&#x27;re essentially trying to do a function call on the literal 6. The workaround was to use a custom codec to wrap all integer literals to give them the expected call behavior.<p>Repo if anyone is interested: <a href="https:&#x2F;&#x2F;github.com&#x2F;ckw017&#x2F;blursed">https:&#x2F;&#x2F;github.com&#x2F;ckw017&#x2F;blursed</a><p>This was inspired by a way less silly usecase, future f-strings, which added f-string support to older versions of Python in a similar way using codecs: <a href="https:&#x2F;&#x2F;github.com&#x2F;asottile-archive&#x2F;future-fstrings">https:&#x2F;&#x2F;github.com&#x2F;asottile-archive&#x2F;future-fstrings</a>
benrutter超过 2 年前
There really is nothing greater than an completely idiotic idea implemented with pure genius - this was immense reading and I learnt a lot!
评论 #34652588 未加载
评论 #34647541 未加载
hsfzxjy超过 2 年前
I&#x27;ve attempted something more crazy -- to make Python support multi-line lambda expression [1].<p>It&#x27;s done with AST manipulation as well, but on a larger scale and completer functionalities.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;hsfzxjy&#x2F;lambdex">https:&#x2F;&#x2F;github.com&#x2F;hsfzxjy&#x2F;lambdex</a>
mjburgess超过 2 年前
So it seems you can register a source transformer with python which will trigger when a `# coding: ...` header is used (to signal character encoding).<p>This seems like an interesting avenue for static analysis tools, code generators, (etc.) to explore. Is it a viable approach?<p>I&#x27;d be interested in some analysis on this as a mechanism: performance, maintability, etc. wise.<p>The &quot;dont do this&quot; argument writes itself; nevertheless, its worth exploring.
评论 #34645409 未加载
评论 #34645924 未加载
评论 #34651242 未加载
ur-whale超过 2 年前
Didn&#x27;t know about codecs ...<p>The idea of using (abusing in fact) codecs to pre-process python code before it gets to the actual python interpreter is just fantastic!<p>I&#x27;m starting to think of all the terrible things I&#x27;m going to be able to do with this to work around things that have annoyed me for years in python.<p>[EDIT]: I&#x27;m thinking one can probably plug the C preprocessor in python now ... oh the sheer joy :D
评论 #34647035 未加载
Mockapapella超过 2 年前
This is the kind of shit I love about python. You can put this in a pre processor script so that it works for every python program you run
___kaukas___超过 2 年前
A long time ago I stumbled upon (and contributed to) <a href="https:&#x2F;&#x2F;github.com&#x2F;delfick&#x2F;nose-of-yeti">https:&#x2F;&#x2F;github.com&#x2F;delfick&#x2F;nose-of-yeti</a>. The most delightful bit of cursed Python I’ve seen!
Awelton超过 2 年前
There is something to be said for doing something just because it&#x27;s ridiculous. It&#x27;s terrible and I love it.
hot_gril超过 2 年前
&quot;It is recognized that you have a funny sense of fun.&quot;
atorodius超过 2 年前
Thanks for this wild ride, it went way further than I anticipated ..
评论 #34651756 未加载
Too超过 2 年前
It’s a bit sad that with-blocks can’t yield more than once. It would be very convenient to write retry code this way.
评论 #34652946 未加载
MathMonkeyMan超过 2 年前
This is evil, and I love it.<p>I wonder if the codec could use python&#x27;s lexer (assuming it&#x27;s exposed) to parse the for loops and nothing else. Then replace the loops with a placeholder, and then replace the placeholder in the AST after a parse. Might be cleaner than source-&gt;source transform by the codec, maybe not.
kristianp超过 2 年前
Where did the &quot;cursed&quot; adjective start being used like this? It&#x27;s not used in my country, and I&#x27;ve only seen it being used in the last couple of years as part of subreddit titles.
akkartik超过 2 年前
Just from the description I think this doesn&#x27;t handle <i>continue</i>.
评论 #34651768 未加载
Too超过 2 年前
Codecs free to transform all code before it’s executed? Sounds like the perfect place for hackers to hide RCEs very difficult to spot. Just hide an innocent comment at the top of a file.
wheelerof4te超过 2 年前
So it&#x27;s true that you can do anything with Python. Even if you shouldn&#x27;t.
StunxFS超过 2 年前
Very good!
winterqt超过 2 年前
(2022)
travisgriggs超过 2 年前
Two things:<p>1. I wish more HN posts ended in &quot;for fun&quot; -- so much of what makes being a progammer fun and enjoyable is plumbing the depths of what is possible, not because it&#x27;s &quot;best practice&quot; or whatever. I think these types of forrays are where true mastery comes from.<p>2. The less reserved keywords a language has, the better. It makes things like this easier. Years ago I figured out how to implement Goto in Smalltalk &quot;for fun&quot;. It was easier because Smalltalk had less reserved keywords.
评论 #34650699 未加载
评论 #34652358 未加载
epr超过 2 年前
Some people have a lot of time on their hands.<p><pre><code> (defmacro cfor [initialize condition advance #* body] `(do ~initialize (while ~condition (do ~@body ~advance)))) (cfor (setv i 0) (&lt; i 10) (+= i 1) (print i)) </code></pre> This runs on the python vm with hy right now. Took about 2 mins to have it up and running after noticing this post on hn. Couldn&#x27;t pass it up.
评论 #34647495 未加载
评论 #34647940 未加载
masklinn超过 2 年前
&gt; Or alternatively: How I made the most cursed Python package of all time.<p>Beg your pardon, <a href="http:&#x2F;&#x2F;entrian.com&#x2F;goto&#x2F;" rel="nofollow">http:&#x2F;&#x2F;entrian.com&#x2F;goto&#x2F;</a> exists.
评论 #34647942 未加载
mixmastamyk超过 2 年前
The range builtin already gives 98% of the functionality, just need to put &quot;i in range&quot; before it.
评论 #34651985 未加载
bb88超过 2 年前
I say this with love: He needs a girlfriend (or life partner)