How to pass a global variable to a module?

Jean-Michel Pichavant jeanmichel at sequans.com
Wed Sep 30 05:31:36 EDT 2009


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



More information about the Python-list mailing list