[Tutor] inter-module global variable
Dave Angel
davea at ieee.org
Mon Mar 29 03:32:57 CEST 2010
spir # wrote:
> On Sun, 28 Mar 2010 21:50:46 +1100
> Steven D'Aprano <steve at pearwood.info> wrote:
>
>
>> On Sun, 28 Mar 2010 08:31:57 pm spir ☣ wrote:
>>
>> <snip>
I'm going to assume you really want a single global value, and that you
won't regret that assumption later.
We talked at length about how to access that global from everywhere that
cares, and my favorite way is with a globals module. And it should be
assigned something like:
globals.py:
class SomeClass (object):
def....
def init(parameters):
global world
world = SomeClass(parameters, moreparamaters)
Then main can do the following:
import globals
globals.init(argv-stuff)
And other modules can then do
import globals.world as world
And they'll all see the same world variable. Nobody should have their
own, but just import it if needed.
More information about the Tutor
mailing list