Mailing List Archive

How to use a variable form main script in a module.
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?

I've been trying to find out how to do it in tutorials an the reference
manual but I can´t find anyting written about this. Perhaps because it's a
to silly question.

/Peter Torstenson

Rio de Janeiro
How to use a variable form main script in a module. [ In reply to ]
Hi Peter,

You need to fully specify the module variable name in your main program.
For example, if an imported module defines x as an unbound (quasi)class
variable:

>>> x = 3
>>> import testx
>>> x
3
>>> testx.x
15

HTH
--

Emile van Sebille
emile@fenx.com
-------------------


Peter Torstenson <p.t@iname.com> wrote in message
news:7nhmt5$su0@enews3.newsguy.com...
> 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?
>
> I've been trying to find out how to do it in tutorials an the
reference
> manual but I can´t find anyting written about this. Perhaps because
it's a
> to silly question.
>
> /Peter Torstenson
>
> Rio de Janeiro
>
>
How to use a variable form main script in a module. [ In reply to ]
* 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.
How to use a variable form main script in a module. [ In reply to ]
* 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.

[....wrong info in previous version of this post removed (global
doesn't work)...]

--Lars M.
How to use a variable form main script in a module. [ In reply to ]
import sys
main= sys.modules['__main__']
main.setDebug(__name__)

larsga@ifi.uio.no (Lars Marius Garshol) wrote in
<wkg12bscly.fsf@ifi.uio.no>:

>
>* 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.
>
>[....wrong info in previous version of this post removed (global
>doesn't work)...]
>
>--Lars M.
>