How to Lock Globar Variables across Modules?

Aahz Maruch aahz at panix.com
Thu Sep 28 18:00:28 CEST 2000


In article <8qt24q$cld$1 at hecate.umd.edu>,
William Djaja Tjokroaminata  <billtj at z.glue.umd.edu> wrote:
>
>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)

If sv points to a class instance, this should work -- as long as you
never change sv to point at a different class instance.  Because sv
points to a mutable object, you can change the interior values as much
as you wish.  This will also provide a nice encapsulation should you
ever wish to handle multiple processes (whether run concurrently in
threads or serially without threads).
-- 
                      --- 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



More information about the Python-list mailing list