有没有可能立即重新加载 Python 模块?

发布于 2024-09-30 20:33:16 字数 412 浏览 1 评论 0原文

导入这些模块后是否可以立即重新加载它们?这是我正在审查的代码,这让我想知道:

import time
import sys
import os
import string
import pp
import numpy
import nrrd
reload(nrrd)
import smooth as sm
reload(sm)
import TensorEval2C as tensPP
reload(tensPP)
import TrackFiber4C as trackPP
reload(trackPP)
import cmpV
reload(cmpV)
import vectors as vects
reload(vects)

编辑:我建议这可能会使 .pyc 文件的创建更有可能,但有几个人指出这种情况第一次发生,每次都会发生。

Is there any conceivable point to reloading these modules immediately after importing them? This is the code that I was reviewing which made me wonder:

import time
import sys
import os
import string
import pp
import numpy
import nrrd
reload(nrrd)
import smooth as sm
reload(sm)
import TensorEval2C as tensPP
reload(tensPP)
import TrackFiber4C as trackPP
reload(trackPP)
import cmpV
reload(cmpV)
import vectors as vects
reload(vects)

Edit: I suggested that this might make the creation of .pyc files more likely, but several people pointed out that this happens this first time, every time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

生寂 2024-10-07 20:33:16

我注意到,标准模块刚刚被导入:其他模块被重新加载。我希望编写这段代码的人都希望能够轻松地重新加载整个包(以便获得他们的最新编辑)。在放入所有这些冗余的 reload 调用之后,程序员只需编写

>>> reload(package)

即可在解释器中更新内容,而不必键入

>>> reload(package.nrrd)
>>> reload(package.sm)
>>> reload(package.tensPP)

等。因此,请忽略您对写这篇文章的程序员:他们远不是唯一一个在重新加载时遇到麻烦的程序员依赖关系。只需鼓励他们将重新加载转移到便利功能即可。

I note that the standard modules are just imported: it's the other modules that are reloaded. I expect whoever wrote this code wanted to be able to easily reload the whole package (so as to get their latest edits). After putting in all these redundant reload calls, the programmer only had to write

>>> reload(package)

to bring things up to date in the interpreter, instead of having to type

>>> reload(package.nrrd)
>>> reload(package.sm)
>>> reload(package.tensPP)

etc. So please ignore the suggestion that you commit violence against the programmer who wrote this: they are far from the only programmer who's had trouble with reloading of dependencies. Just encourage them to move the reloads to a convenience function.

孤单情人 2024-10-07 20:33:16

这有可能确实导致某些事情发生;一个明显的例子是导入时发生的副作用。例如,模块可以将每次导入的时间和日期记录到文件中。

然而,这可能没有充分的理由。

It is possible that this does cause something to happen; the obvious example is side-effects that happen on import. For instance, a module could log to a file the time and date of every time it is imported.

There is probably no good reason for this, however.

℉絮湮 2024-10-07 20:33:16

.pyc 文件将在第一次导入时创建,因此即使这也不是一个很好的理由。

The .pyc files would be created on the first import, so even that's not a very good reason for this.

扭转时空 2024-10-07 20:33:16

这段代码的执行环境是什么?至少存在一个 Python Web 框架,它会做出与标准 Python 不同的重新加载决策,当您做出不“接受”的更改时,这会导致沮丧和困惑。

What's the execution environment for this code? There exists at least one Python web framework that makes different reload decisions than standard python does, which leads to frustration and confusion when you make a change that doesn't 'take'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文