How to use a variable form main script in a module.

Lars Marius Garshol larsga at ifi.uio.no
Mon Jul 26 10:31:29 EDT 1999


* Peter Torstenson
|
| This is probaly a silly question from a newbie.
| In my main script I put a value for to a variable called "x".
| Now I want to use the "x" variable in a module.
| The module is imported into the main script. How to do this?

>From the way you describe it this sounds like something you shouldn't
do. Communication between different pieces of code through globabl
variables is in general frowned upon, and with good reason. It's good
for some things, but for those the variables tend to reside in a
module, and not in the global namespace.

Feel free to explain the reason why you want this if you want a
verdict on whether it's a good or bad idea.

If you still want to do this you can use the 'global' keyword, like
so:

def myfunc(blah):
  global var
  return var+blah

--Lars M.




More information about the Python-list mailing list