[英]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.