所以我讀亞歷馬爾泰利的答案other question ...Python:我可以在包__init__模塊中設置全局變量嗎?
「中,我可能要初始化一個例子是在包加載時間,當我想在一堆數據的一勞永逸閱讀(來自文件,數據庫或網頁) - 在這種情況下,將該讀取放在程序包的init .py中的私有函數中更好,而不是單獨具有「初始化模塊」並冗餘地導入模塊從包裝中的每個真實模塊...「
不幸的是,當我試試這個:
富/ __ init__.py
import tables as tb
global foo
foo = tb.openFile('foo.h5', etc._)
import bar
富/酒吧/ __ init__.py
import tables as tb
global bar
bar = foo.createGroup('/', bar)
import MyFunction`
富/酒吧/ MyFunction.py
def MyFunction(*of foo and bar*):
'...'
>>> import foo
>>> OUTPUT= foo.bar.MyFunction.MyFunction(INPUT)
>>> bar = foo.createGroup('/', bar)
NameError: name 'foo' is not defined
如何定義全局變量而不將它們放入函數中(如所見here)?
當粘貼代碼縮進四個空格時,您需要做的全部操作。甚至有一個按鈕(帶有鍵盤快捷鍵)可以爲你做到。 –
@Marcelo Cantos我試過了,相信我。我不知道這個東西的格式是什麼,但它似乎從不想與我合作。 –