How to pass a global variable to a module?

M

Mars creature

Dear Python users,
I just start to use python and love this language. I met this
problem when I try to save my functions in a separate file.
The question is how I can pass a global variable to a function which
is saved in another file. If I save the function I defined in the same
file with the main program, there is no problem after I declare the
global variable. But problem comes out when I save all the function is
a separate file. Help is very much appreciated! Thanks!
Jinbo
 
R

Rami Chowdhury

Dear Python users,
I just start to use python and love this language. I met this
problem when I try to save my functions in a separate file.
The question is how I can pass a global variable to a function which
is saved in another file. If I save the function I defined in the same
file with the main program, there is no problem after I declare the
global variable. But problem comes out when I save all the function is
a separate file. Help is very much appreciated! Thanks!
Jinbo

In Python, as in many other languages, I'd advise that you think about
whether your variable needs to be global, or whether you could (or should)
simply pass the variable to the function as a parameter.

HTH,
Rami
 
G

Gregor Horvath

Hi,

Am Tue, 29 Sep 2009 09:40:29 -0700 (PDT)
schrieb Mars creature said:
I just start to use python and love this language. I met this
problem when I try to save my functions in a separate file.
The question is how I can pass a global variable to a function which
is saved in another file. If I save the function I defined in the same
file with the main program, there is no problem after I declare the
global variable. But problem comes out when I save all the function is
a separate file. Help is very much appreciated! Thanks!
Jinbo

http://mail.python.org/pipermail/tutor/2002-November/018353.html
 
J

Jean-Michel Pichavant

Mars said:
Dear Python users,
I just start to use python and love this language. I met this
problem when I try to save my functions in a separate file.
The question is how I can pass a global variable to a function which
is saved in another file. If I save the function I defined in the same
file with the main program, there is no problem after I declare the
global variable. But problem comes out when I save all the function is
a separate file. Help is very much appreciated! Thanks!
Jinbo
Do not use global variable, that's evil !

in file1.py:

myVar = 'foo'


in file2.py:
import file1

print file1.myVar
file1.myVar = 'bar'
print file1.myVar
Keep your variables ordered on their shelf.

JM
 
M

Mars creature

In Python, as in many other languages, I'd advise that you think about  
whether your variable needs to be global, or whether you could (or should)  
simply pass the variable to the function as a parameter.

HTH,
Rami

--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --  
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)

Thank you guys for the prompt and helpful response.
From the link Gregor posted, it seems no way to share variable between
modules.

I can understand the point that global variables tends to mess up
programs.

Assume that I have 10 parameters need to pass to the function. If
these parameters are fixed, I can use another module to store these 10
parameters, and import to the module, as suggested by jean-michel. But
what if these 10 parameters will be changed in the main program?
Passing the variable to the function as a parameter suggested by Rami
will certainly do, but I am wondering weather there are other ways.
What you'd like to code it?
Thank you very much!
Jinbo
 
M

MRAB

Mars said:
Thank you guys for the prompt and helpful response.
modules.

I can understand the point that global variables tends to mess up
programs.

Assume that I have 10 parameters need to pass to the function. If
these parameters are fixed, I can use another module to store these 10
parameters, and import to the module, as suggested by jean-michel. But
what if these 10 parameters will be changed in the main program?
Passing the variable to the function as a parameter suggested by Rami
will certainly do, but I am wondering weather there are other ways.
What you'd like to code it?

If there are a lot of them then an alternative is to pass them in some
sort of contains, such as a dict or an object:
'foo'
 
T

Terry Reedy

Mars said:
I just start to use python and love this language. I met this
problem when I try to save my functions in a separate file.
The question is how I can pass a global variable to a function which
is saved in another file.

This question is somewhat mis-phrased. In Python, one uses names --
local, nonlocal, global, and dotted -- as well as other expressions, to
pass objects to functions as arguments that get bound to the parameter
names of the function. Objects are neither local or global; they just
are. Thinking this way will help your use of Python.
> If I save the function I defined in the same
file with the main program, there is no problem after I declare the
global variable. But problem comes out when I save all the function is
a separate file. Help is very much appreciated! Thanks!

Specific answers require specific examples and either a clear
description of actual versus expected behavior or a complete copy of an
error traceback. Good luck.

tjr
 
D

Dave Angel

Mars said:
<snip>
I can understand the point that global variables tends to mess up
programs.

Assume that I have 10 parameters need to pass to the function. If
these parameters are fixed, I can use another module to store these 10
parameters, and import to the module, as suggested by jean-michel. But
what if these 10 parameters will be changed in the main program?
Passing the variable to the function as a parameter suggested by Rami
will certainly do, but I am wondering weather there are other ways.
What you'd like to code it?
Thank you very much!
Jinbo
If we're just talking generalities, we can give you general advice.
Avoid globals like the plague. Except for constants, each global should
require a lot of justification to permit its use. There's no harm in
passing 10 parameters to a function. And if some of them are related to
each other, group them in a tuple, or an object. If two functions seem
to have a need to share data without passing it back and forth, they
probably belong in a class.

Most of the justifiable globals are already there in the standard
libraries, or at least a good analogy. For example, stdout is used by
print, wherever it occurs. Likewise you may want a global logging
object. These are things which act a lot like constants, even though
they have internal state.

DaveA
 
A

alex23

Mars creature said:
Assume that I have 10 parameters need to pass to the function. If
these parameters are fixed, I can use another module to store these 10
parameters, and import to the module, as suggested by jean-michel. But
what if these 10 parameters will be changed in the main program?

With Python, for the duration of program execution a module is
(generally) only ever imported by an import statement once. Any other
imports will instead look up the result of the first import, and will
refer to that module.

So you can use modules to stash variables for the life of the program:

a.py:

import globals

globals.a = 1
globals.b = 2

b.py:

import globals

globals.b = 77

c.py:

import globals

print globals.a, globals.b # will be '1, 77'
Passing the variable to the function as a parameter suggested by Rami
will certainly do, but I am wondering weather there are other ways.
What you'd like to code it?

While you can use globals to avoid having to pass functions around,
it's generally a sign of bad code. You're tightly binding the
behaviour of the function to the presence of the globals, which makes
the function a lot harder to re-use elsewhere.

Depending on the functions, I'd tend to use either a list or a dict:
.... pass
....
params = [1, 3, 5, 7, 9]
positional_args_func(*params) # the * unpacks the list

def keyword_args_func(a=None, b=None, c=None, d=None, e=None):
.... pass
....
If you're using Python 2.6/3.x, you might find namedtuple handy:
(1, 9)
.... # access through params.a, params.b etc
.... pass
....
Or course, any combination of these can be used in the same functions.

Hope this helps.
 
H

Hendrik van Rooyen

modules.

I can understand the point that global variables tends to mess up
programs.

Assume that I have 10 parameters need to pass to the function. If
these parameters are fixed, I can use another module to store these 10
parameters, and import to the module, as suggested by jean-michel. But
what if these 10 parameters will be changed in the main program?
Passing the variable to the function as a parameter suggested by Rami
will certainly do, but I am wondering weather there are other ways.
What you'd like to code it?

You can put all the 10 things in a file called say my_params.py.

Then where you need it, you do either:

from my_params import * to make them available where needed,

or:

import my_params as p and access them as:

print p.my_parm_1,p.my_parm_2,p.my_parm_3,p.my_parm_4

- Hendrik
 
J

Jean-Michel Pichavant

Mars said:
Thank you guys for the prompt and helpful response.
modules.

I can understand the point that global variables tends to mess up
programs.

Assume that I have 10 parameters need to pass to the function. If
these parameters are fixed, I can use another module to store these 10
parameters, and import to the module, as suggested by jean-michel. But
what if these 10 parameters will be changed in the main program?
Passing the variable to the function as a parameter suggested by Rami
will certainly do, but I am wondering weather there are other ways.
What you'd like to code it?
Thank you very much!
Jinbo
Why don't you post the function you're trying to code, with the
parameter names ?
Write the documentation for that function, write what it is supposed to
do, the parameters, their purpose and the returned value. Just by doing
this, you may be able to find all by yourself what should be the correct
function prototype.

JM
 
M

Mars creature

Why don't you post the function you're trying to code, with the
parameter names ?
Write the documentation for that function, write what it is supposed to
do, the parameters, their purpose and the returned value. Just by doing
this, you may be able to find all by yourself what should be the correct
function prototype.

JM

The function I am trying to code is quite simple and nothing special.
I guess what I wanted to say was how to avoid typing all parameters
everytime I am using the function. I used to use common block in
Fortran to keep the frequently used data. I could've put all
parameters in a file and import it, if they are unchangable. But in my
case the parameters are changing.

Allow me to say, unpacking the list or dictionary is the answer I
wanted, although this is too trivial for some of you.

Based on the discussion (correct me if I'm wrong),
1, try to avoid global,
2, if parameters are constant, put them in a tuple/list/dictionary and
import them
3, if parameters are changeable, pack them into a list/dictionary and
use *params (for list) or **params (for dict) to unpack and pass to
the function.

I want to thank you all! It's quite bit learning for me from your
discussion.
Jinbo
 
M

Mel

Mars said:
The function I am trying to code is quite simple and nothing special.
I guess what I wanted to say was how to avoid typing all parameters
everytime I am using the function. I used to use common block in
Fortran to keep the frequently used data. I could've put all
parameters in a file and import it, if they are unchangable. But in my
case the parameters are changing.

Write a function that calls the function you want to call, taking the
arguments you want to retype, and filling in all the arguments you don't:

def stand_in (great, nifty):
call a_function (bo, great, ri, nifty, ng)


Mel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top