user2554925
user2554925

Reputation: 487

Working with global variables in multiple classes and multiple modules

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?

Upvotes: 2

Views: 1774

Answers (1)

steliosbl
steliosbl

Reputation: 8921

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

Upvotes: 2

Related Questions