How to pass a global variable to a module?
davea at ieee.org
Wed Sep 30 00:19:29 CEST 2009
Mars creature wrote:
> I can understand the point that global variables tends to mess up
> 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!
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.
More information about the Python-list