我的问题是如何在python中初始化全局字符串变量 例如,当我执行以下操作时。
24 def global_paths():
25 global RUN_PATH
26 global BASE_PATH
27 global EXE_SUFIX
28 global SPEC_PATH
29 global cmd_list
30
31 global RUN_PATH = "/run/run_base_ref_amd64-m64-gcc43-nn.0000/"
32 global BASE_PATH = "/SPECcpu2006/1.1/cdrom"
33 global EXE_SUFIX = "_base.amd64-m64-gcc43-nn"
34 global SPEC_PATH = BASE_PATH + "/benchspec/CPU2006/"
35 global cmd_list = {}
我收到错误:
global RUN_PATH = "/run/run_base_ref_amd64-m64-gcc43-nn.0000/"
^
SyntaxError: invalid syntax
我在做什么错误?
问题类似于this
答案 0 :(得分:6)
创建全局变量时,无需添加额外的global
。您只需要在创建变量之前对其进行全球化(就像您所做的那样),然后您可以正常创建它:
def global_paths():
global RUN_PATH
global BASE_PATH
global EXE_SUFIX
global SPEC_PATH
global cmd_list
RUN_PATH = "/run/run_base_ref_amd64-m64-gcc43-nn.0000/"
BASE_PATH = "/SPECcpu2006/1.1/cdrom"
EXE_SUFIX = "_base.amd64-m64-gcc43-nn"
SPEC_PATH = BASE_PATH + "/benchspec/CPU2006/"
cmd_list = {}
答案 1 :(得分:2)
Global不用于定义具有全局上下文的变量,而是将全局命名空间中已定义的变量标记为当前执行上下文的全局变量,在您的情况下将是global_paths函数