How to pass a global variable to a module?

Terry Reedy tjreedy at udel.edu
Tue Sep 29 21:41:24 CEST 2009


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





More information about the Python-list mailing list