Is there any possible point to reloading a Python module immediately?

白昼怎懂夜的黑 提交于 2019-12-10 15:25:07

问题


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.


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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'.



来源:https://stackoverflow.com/questions/4127647/is-there-any-possible-point-to-reloading-a-python-module-immediately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!