2

If I create a global variable in a function in a class, how can I use that variable in another function in a different class?

NB: Classes are found in different modules.

Do I need to declare the global variables in the init function of each class?

user2554925
  • 487
  • 2
  • 8

1 Answers1

2

Global vars can be accessed throughout the module they are declared in. To access a global variable declared in one module from within another one, simply do:

Module1.py:

global foo
foo = "bar"

Module2.py:

import Module1
Module1.foo
stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53