2013-05-07 76 views
1

我做两个文件:如何获取inspect.getsource以响应源代码中的更改?

#test_func.py 
def test(): 
    print('hello') 

#test_inspect.py 
import inspect 
import test_func 

reload(inspect) 
reload(test_func) 
reload(inspect) 
reload(test_func) 

print inspect.getsource(test_func.test) 

运行从IPython中或其它交互shell打印出正确的事情import test_inspect

def test(): 
    print('hello') 

,但如果我编辑和保存test_func。 py将是:

#test_func.py 
def test(): 
    print('good bye') 

我仍然得到:

def test(): 
    print('hello') 

当我从shell中运行reload(test_inspect)。我如何说服inspect模块重新读取源文件?

(如果您需要必须知道我为什么要这样做,我会详细说明评论,但现在我想简单地知道是否有解决方法,或者是否存在根本性问题关于防止这个检查模块)

回答

3

这应该做的伎俩:

import linecache 
linecache.clearcache() 
+0

啊。好多了。谢谢。 – Paul 2013-05-07 00:51:22