发布于 2015-08-30 07:51:20 | 173 次阅读 | 评论: 0 | 来源: 网络整理

问题

You want to reload an already loaded module because you’ve made changes to its source.


解决方案

To reload a previously loaded module, use imp.reload(). For example:

>>> import spam
>>> import imp
>>> imp.reload(spam)
<module 'spam' from './spam.py'>
>>>

讨论

Reloading a module is something that is often useful during debugging and development, but which is generally never safe in production code due to the fact that it doesn’t always work as you expect.

Under the covers, the reload() operation wipes out the contents of a module’s underlying dictionary and refreshes it by re-executing the module’s source code. The identity of the module object itself remains unchanged. Thus, this operation updates the module everywhere that it has been imported in a program.

However, reload() does not update definitions that have been imported using statements such as from module import name. To illustrate, consider the following code:

# spam.py
def bar():
    print('bar')

def grok():
    print('grok')

Now start an interactive session:

>>> import spam
>>> from spam import grok
>>> spam.bar()
bar
>>> grok()
grok
>>>

Without quitting Python, go edit the source code to spam.py so that the function grok() looks like this:

def grok():
    print('New grok')

Now go back to the interactive session, perform a reload, and try this experiment:

>>> import imp
>>> imp.reload(spam)
<module 'spam' from './spam.py'>
>>> spam.bar()
bar
>>> grok() # Notice old output
grok
>>> spam.grok() # Notice new output
New grok
>>>

In this example, you’ll observe that there are two versions of the grok() function loaded. Generally, this is not what you want, and is just the sort of thing that eventually leads to massive headaches.

For this reason, reloading of modules is probably something to be avoided in production code. Save it for debugging or for interactive sessions where you’re experimenting with the interpreter and trying things out.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务