2013-05-07 73 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