[英]How to declare a global variable in a module
在主程序中
import medallia_utils
from collections import defaultdict
Opers = defaultdict(int)
在此程序的后面,我將在Medellia_utils中調用一個名為word_checker的函數。
我想在badgelia_utils內部定義的word_checker函數中操作字典變量Opers,因此我想將其聲明為Global。
當我在badgelia_utils內部執行此操作時:
def word_checker(name, comment):
import re
from collections import defaultdict
global Opers
系統說
NameError: global name 'Opers' is not defined
我還嘗試通過在word_checker函數外部的badgelia_utils中全局定義此變量
global Opers
要么
global __Opers__
但到目前為止沒有任何效果。 我犯了同樣的錯誤。
使它起作用的語法修補程序是什么?
沒有可以使這項工作的“語法修復”。 如果要使用另一個模塊中定義的變量,則需要導入它。
在將其聲明為全局變量之后,需要使該變量保留某些內容。
global name
name = 'bob'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.