persistant gloabl vars (very newbie) ?

Erik Johnson ejatwellkeeperdotcom
Wed Dec 27 12:35:34 EST 2006


"Stef Mientki" <S.Mientki-nospam at mailbox.kun.nl> wrote in message
news:bdff8$45929445$d443bb3a$5918 at news.speedlinq.nl...

> Is there a way to run the initialization code from a script(file) once,
> to achieve the same effect ?

Certainly. This is what Python modules are all about. You should probably
read up on those a bit here: http://docs.python.org/tut/node8.html

But briefly, probably what you want to do is put some code in a file, say
init.py:

    # init.py
    X = 3
    Y = 5
    # A bunch of other stuff


And then in your main program, execute

    from init import *

That will take all the module-scoped variables defined in init.py and place
them in the namespace of the import statement (whcih could be global, the
interactive interpreter, or otherwise)

See also the 'global' statement as it relates to modifying global variables
in an inner scope.

Good luck,
-ej





More information about the Python-list mailing list