是否有可能立即重新加载Python模块? [英] Is there any possible point to reloading a Python module immediately?

查看:147
本文介绍了是否有可能立即重新加载Python模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在导入模块后立即重新加载这些模块?这是我正在审查的代码,这使我感到奇怪:

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)

我建议这样做可能使创建.pyc文件的可能性更大,但是一些人指出,这是第一次,每次都发生.

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.

推荐答案

我注意到 standard 模块只是导入的:其他模块已被重新加载.我希望编写此代码的人都希望能够轻松地重新加载整个程序包(以便获取其最新编辑信息).在进行了所有这些多余的reload调用之后,程序员只需要编写

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.

这篇关于是否有可能立即重新加载Python模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆