1

How can I define a global variable.

For example I have Foo1 class and Foo2 class and I want to use FooVariable in this classes as a global variable.

Rob
  • 26,989
  • 16
  • 82
  • 98
Mokus
  • 10,174
  • 18
  • 80
  • 122

1 Answers1

2

If you wanted a global variable named foo, you would just have to put global foo at the top of any function in which it is used. For example,

def do_something():
    global foo
    foo = foo + 1
    print foo

Note that Python "global" variables are only global to the module they appear in, not global to the entire program (as in some languages).

Jeremy
  • 1
  • 85
  • 340
  • 366