我建議您將該變量保留在全局範圍內,並使用global聲明訪問它。
關鍵字global允許您引用全局名稱空間中的變量,儘管在函數調用方中存在另一個具有相同名稱的變量。
例如:這將打印 「橘子」
def foo():
def bar():
return fruit
fruit = "tangerine"
return bar2()
fruit = "banana"
print(foo()) # will print "tangerine"
但是,這將打印 「香蕉」。
def foo2():
def bar2():
global fruit # fetch me the fruit in the global scope
return fruit
fruit = "tangerine"
return bar2()
fruit = "banana"
print(foo2()) # will print "banana"
沒有全球性的關鍵字,解釋器將遵循「LEGB規則」,以便找到你想引用變量:
- 大號 OCAL範圍:第一,它是將搜索本地作用域中的變量,即函數語句正在執行的作用域。本地範圍在每次調用函數時創建。
- E nclosing作用域:其次,解釋器將在「封閉函數本地」中查找變量,即函數插入(聲明)的函數。這就是水果變量返回水果裏面的發現bar()。
- G lobal:如果在上面的命名空間中找不到變量,python會嘗試在全局變量中找到它。在全球聲明力量蟒蛇的使用情況,以尋找它只能在全球範圍內,因此跳過1個2
- 乙 uiltins:最後,蟒蛇將試圖找到內置功能中的變量和常量。這裏沒有魔法。
如果所有的搜索上面無法找到變量名,Python會引發一個NameError例外:
$ NameError: name 'fruit' is not defined
然後,你可以這樣做:
def choosingFunction():
global option
if option == 0:
doThis()
elif option == 1:
doThat()
elif option == 2:
doTheOtherOne()
else:
doWhateverYouFeelLike()
>> option = 2
>> import choosingModule
>> choosingModule.choosingFunction()
記住在調用你的模塊中依賴全局變量的存在被認爲是一種非常糟糕的做法,不得嚴重使用它。 ;)
有關全球聲明的更多信息,請查閱文檔:Python 3.x和Python 2.7。 有關範圍和名稱空間規則的信息,請檢查here(Python 3.x)或here(Python 2.7)。
來源
2012-04-20 05:01:11
FRD
您的第一個解決方案給我錯誤消息「AttributeError:'FakeModule'對象沒有屬性'選項'」。 – thornate 2012-04-20 04:31:44
@thornate:這看起來像是一個IPython的東西。不幸的是,我無法複製那個錯誤。 – icktoofay 2012-04-20 04:36:01