How to Lock Globar Variables across Modules?

William Djaja Tjokroaminata billtj at z.glue.umd.edu
Wed Sep 27 11:01:46 EDT 2000


Aahz Maruch <aahz at panix.com> wrote:
: In article <8qnqpa$p4f$1 at hecate.umd.edu>,
: William Djaja Tjokroaminata  <billtj at y.glue.umd.edu> wrote:
:>
:>I am having this problem.  Suppose in 'module1.py' I declare a global
:>variable named 'globvar_mod1'.  In another module, called 'module2.py', I
:>have the statement 'from module import *'.
:>
:>It looks like when I am running the program, the global variable in code
:>in module2 only refers to the initial value in module1, unless I use an
:>explicit name.  

: "Doctor, it hurts when I bang my head into the wall."
: "Well, then, don't do that."

: Same principle applies here.  What you're trying to do doesn't fit well
: with Python's model -- nor does it fit well with good programming style.
: If you explain what your goal is, we may be able to give a better way to
: accomplish it.  Meanwhile, I suggest you stick with module.var_name.
: -- 
:                       --- Aahz (Copyright 2000 by aahz at pobox.com)

: Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
: Hugs and backrubs -- I break Rule 6

: "Perhaps God rewards martyrs, but life seldom does..." --Ulrika O'Brien

------------------------------------------------------

I am creating a network simulation tool in python.  It is based on
discrete-event simulation model.  In the simulation, there is only one
"process" that can run at one time (no modeling of thread in the
simulation).  Because I follow the function call model in another
commercial simulation tool, all the function calls do not refer to the
currently running process.  I figured out that I can create exactly the
same function call formats by putting the currently running process in a
global variable in the main module (hence 'globvar_mod1').

As I use inheritance in other modules, I need to refer the currently
running process.  I have read many times that global variables are
dangerous.  But I think if we use it judiciously, it can make the
programming simpler.  I have only one global variable; for example, it is
simply called 'sv'.  In the main module, all the code will look like

    schedule (sv.current_time)

But the same code in other modules will have to look like

    schedule (main_module.sv.current_time)

I want something that is consistent across all the simulation
modules.  Using function call as suggested by another person is not too
atractive to me because it destroys the consistency (and also longer).  If
possible, I want also to reduce the amount of typing in the other modules
if I can just refer to the variable as 'sv' and not 'main_module.sv' :)


Regards,

Bill



More information about the Python-list mailing list