Re: How to pass a global variable to a module?

2009-09-30 Thread Hendrik van Rooyen
On Tuesday, 29 September 2009 20:24:53 Mars creature wrote:

 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?

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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass a global variable to a module?

2009-09-30 Thread Jean-Michel Pichavant

Mars creature wrote:

On Sep 29, 12:49 pm, Rami Chowdhury rami.chowdh...@gmail.com
wrote:
  

On Tue, 29 Sep 2009 09:40:29 -0700, Mars creature jin...@gmail.com wrote:


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

--
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
  
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
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass a global variable to a module?

2009-09-30 Thread Mars creature
On Sep 30, 5:31 am, Jean-Michel Pichavant jeanmic...@sequans.com
wrote:
 Mars creature wrote:
  On Sep 29, 12:49 pm, Rami Chowdhury rami.chowdh...@gmail.com
  wrote:

  On Tue, 29 Sep 2009 09:40:29 -0700, Mars creature jin...@gmail.com wrote:

  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

  --
  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

 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass a global variable to a module?

2009-09-30 Thread Mel
Mars creature wrote:

 On Sep 30, 5:31 am, Jean-Michel Pichavant jeanmic...@sequans.com
 wrote:
 Mars creature wrote:
  On Sep 29, 12:49 pm, Rami Chowdhury rami.chowdh...@gmail.com
  wrote:

  On Tue, 29 Sep 2009 09:40:29 -0700, Mars creature jin...@gmail.com
  wrote:

  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

  --
  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

 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.

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.


-- 
http://mail.python.org/mailman/listinfo/python-list


How to pass a global variable to a module?

2009-09-29 Thread 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass a global variable to a module?

2009-09-29 Thread Gregor Horvath
Hi,

Am Tue, 29 Sep 2009 09:40:29 -0700 (PDT)
schrieb Mars creature jin...@gmail.com:

   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

--
Gregor
http://gregor-horvath.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass a global variable to a module?

2009-09-29 Thread Jean-Michel Pichavant

Mars creature wrote:

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
 'foo'

file1.myVar = 'bar'
print file1.myVar
 'bar'

Keep your variables ordered on their shelf.

JM
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass a global variable to a module?

2009-09-29 Thread Rami Chowdhury

On Tue, 29 Sep 2009 09:40:29 -0700, Mars creature jin...@gmail.com wrote:


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

--
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)
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass a global variable to a module?

2009-09-29 Thread Mars creature
On Sep 29, 12:49 pm, Rami Chowdhury rami.chowdh...@gmail.com
wrote:
 On Tue, 29 Sep 2009 09:40:29 -0700, Mars creature jin...@gmail.com wrote:
  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

 --
 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass a global variable to a module?

2009-09-29 Thread MRAB

Mars creature wrote:

On Sep 29, 12:49 pm, Rami Chowdhury rami.chowdh...@gmail.com
wrote:

On Tue, 29 Sep 2009 09:40:29 -0700, Mars creature jin...@gmail.com wrote:

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

--
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?


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:

 class Params(object):
pass

 params = Params()
 params.x = 'foo'
 params.x
'foo'

--
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass a global variable to a module?

2009-09-29 Thread Terry Reedy

Mars creature wrote:

  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


--
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass a global variable to a module?

2009-09-29 Thread Dave Angel

Mars creature wrote:

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

--
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass a global variable to a module?

2009-09-29 Thread alex23
Mars creature jin...@gmail.com wrote:
 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:

 def positional_args_func(a, b, c, d, e):
... 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
...
 params = dict(a=1, b=3, c=5, d=7, e=9)
 keyword_args_func(**params) # the ** unpacks the dict


If you're using Python 2.6/3.x, you might find namedtuple handy:

 from collections import namedtuple
 Parameters = namedtuple('Parameters', 'a b c d e')
 params = Parameters(1, 3, 5, 7, 9)
 params
Parameters(a=1, b=3, c=5, d=7, e=9)
 params.a, params.e
(1, 9)

 def singular_arg_func(params):
... # access through params.a, params.b etc
... pass
...
 singular_arg_func(params)


Or course, any combination of these can be used in the same functions.

Hope this helps.
-- 
http://mail.python.org/mailman/listinfo/python-list