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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Show HN: Change Python code while it's running using a reloading loop

187 点作者 julvo超过 5 年前

15 条评论

uryga超过 5 年前
i wrote something similar a while ago, and it gave rise to a pretty head-scratching bug... my code was doing something like<p><pre><code> if type(x) is Foo: </code></pre> and mysteriously failed for some data around the reload. turns out that Foo objects from before the reload were using a different class object than Foo objects after the reload (which makes sense in hindsight, as Foo was redefined). fun times debugging that!
评论 #21294262 未加载
评论 #21295675 未加载
throwaway66920超过 5 年前
When I first read this I couldn’t think of a good use case. Then the example shown seemed like something I deal with all the time and wish I could do. But then I realized I would need to remember to add reloading to the range before I commit to the long loop I wish to change. Pretty damn cool though.<p>If it doesn’t add much overhead this would be amusing for something like game dev, swapping out how the game works while running it.
评论 #21295665 未加载
评论 #21293024 未加载
Torwald超过 5 年前
Like ESR said: &quot;these languages [perl, python, ruby] subsequently become more like lisp&quot; (paraphrasing)
评论 #21291649 未加载
评论 #21291012 未加载
ovi256超过 5 年前
This is great work, I could not believe the fact that it keeps state (like the current epoch number, and of course all the current model state). Makes Python even more dynamic.<p>Nice that you could condense it to less than 100 lines of Python!
deehouie超过 5 年前
This is a great debugging tool for deep learning. Often your code might have been running for 20 hrs on 16 GPUs and you just don&#x27;t want to restart from scratch again.
评论 #21294444 未加载
noobiemcfoob超过 5 年前
Really nice tool! I often develop in a similar pattern (though not quite as functional) using IPython and it&#x27;s %autoreload magic. This looks a lot cleaner.
kaffeemitsahne超过 5 年前
Could be very nice for live coding music&#x2F;visuals etc.
technicalbard超过 5 年前
Ah, something Common Lisp has had for over 30 years.
civilian超过 5 年前
It&#x27;d be cool to be able to use this as a method decorator too!
评论 #21294071 未加载
PerryCox超过 5 年前
Anyone else getting this error? I tried on Debian 9 and MacOS 10.14.6 after installing via pip.<p><pre><code> import reloading Traceback (most recent call last): File &quot;&lt;input&gt;&quot;, line 1, in &lt;module&gt; import reloading File &quot;&#x2F;home&#x2F;lucas.mccoy&#x2F;.local&#x2F;lib&#x2F;python2.7&#x2F;site-packages&#x2F;reloading&#x2F;__init__.py&quot;, line 1, in &lt;module&gt; from .reloading import reloading File &quot;&#x2F;home&#x2F;lucas.mccoy&#x2F;.local&#x2F;lib&#x2F;python2.7&#x2F;site-packages&#x2F;reloading&#x2F;reloading.py&quot;, line 87 exc = exc.replace(&#x27;File &quot;&lt;string&gt;&quot;&#x27;, f&#x27;File &quot;{fpath}&quot;&#x27;) ^ SyntaxError: invalid syntax</code></pre>
评论 #21290451 未加载
yomansat超过 5 年前
FYI there&#x27;s also reload(module) which is a built-in function. One great use-case I&#x27;ve come across is in developing a network proxy for game packet interception, see:<p><a href="https:&#x2F;&#x2F;youtu.be&#x2F;iApNzWZG-10?t=641" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;iApNzWZG-10?t=641</a>
Waterluvian超过 5 年前
Django&#x27;s development mode aka &quot;runserver&quot; behaves a lot like this (as does a ton of different Node&#x2F;JavaScript systems, many of which are much more precise on what sub-components to actually re-load). It&#x27;s a big win for development pacing.<p>Being able to loop a specific segment of code looks like a great tool for my toolbox when writing data processing code, particularly for spatial data. I will have an input dataset and be iterating on the algorithm(s) as I watch the output (often rendered in QGIS) update dynamically but requiring me to re-run the test script.
评论 #21293714 未加载
_carl_jung超过 5 年前
This is awesome. A nice improvement would be to have the loop continue with the old code when there are errors (rather than pausing iteration).
评论 #21293175 未加载
评论 #21290631 未加载
kazinator超过 5 年前
Change Bourne shell code while it&#x27;s running:<p><pre><code> $ vi ~&#x2F;.functions # fix $ . ~&#x2F;.functions # load latest</code></pre>
potta_coffee超过 5 年前
Python is almost as good as Lisp now.
评论 #21298714 未加载