我有一個用於代碼優化的timit函數的問題。例如,我在文件中寫入功能與參數,讓我們稱之爲myfunctions.py
包含:Python Timeit和「全局名稱...未定義」
def func1(X):
Y = X+1
return Y
,我在第二個文件test.py
,我調用計時器功能測試代碼性能(顯然更復雜的測試此功能問題),包含:
import myfunctions
X0 = 1
t = Timer("Y0 = myfunctions.func1(X0)")
print Y0
print t.timeit()
的Y0
不計算,即使我評論print Y0
線錯誤global name 'myfunctions' is not defined
發生。
如果我用此命令指定
t = Timer("Y0 = myfunctions.func1(X0)","import myfunctions")
現在錯誤global name 'X0' is not defined
發生的設置。
有人知道如何解決這個問題嗎?非常感謝。
[使用Python的timeit獲取「全局名稱foo'未定義」的可能重複](https://stackoverflow.com/questions/551797/getting-global-name-foo-is-not-defined-with -python-timeit) – sds 2017-09-20 16:21:15