Globals

Michal Wallace sabren at manifestation.com
Wed Oct 16 13:41:27 EDT 2002


On Wed, 16 Oct 2002, Newt wrote:

> global strVar
> global iVar
> 
> Questions:
> 
> 1. Is this the correct/best way?
> 2. To save typing in all the global statements, can I put them in to an
> include file or a source file?
> 3. If I declare them in a .py file, do I still need the global commands to
> use them within Classes and Functions etc defined within that same module?

If I really need a global variable (and when I think I do, I
probably don't), I tend to put it in a separate module, say
config.py and then type "import config; print config.myvar"

I do tend to use global constants from time to time. Python
doesn't really have constants, so I just name variables in
ALLCAPS as a convention. Since they're constants, you can
use them anywhere in the same source file, and never need
the "global" statement. You only use "global" to assign to a
global variable. You can always read from it. (It's a little
trickier if you have a local variable with the same name,
but why would anyone do that? <g>)

Does that help at all?

Cheers,

- Michal   http://www.sabren.net/   sabren at manifestation.com 
------------------------------------------------------------
Switch to Cornerhost!             http://www.cornerhost.com/
 Low Priced, Reliable Blog Hosting, With a Human Touch. :)
------------------------------------------------------------





More information about the Python-list mailing list