2

My question is how do initialize global string variables in python For example, when I do the following.

 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  = {}

I get the error:

    global RUN_PATH  = "/run/run_base_ref_amd64-m64-gcc43-nn.0000/"
                     ^
SyntaxError: invalid syntax

What is the mistake I'm doing?

Question is similar to this

Community
  • 1
  • 1
pistal
  • 2,310
  • 13
  • 41
  • 65

2 Answers2

6

You don't need to add the extra global when creating global variables. You only need to globalise it before you create the variable (as you have done), and then you can create it normally:

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  = {}
TerryA
  • 58,805
  • 11
  • 114
  • 143
2

Global is not used to define a variable with global context, but to make an already defined variable in the global namespace to be marked as global to your current execution context, which in your case will be the global_paths function

Goncalo
  • 362
  • 4
  • 8