My scope is broke

Travis Beaty t.beaty at mchsi.com
Fri Dec 20 14:24:31 EST 2002


Hello all!

Time for another "confused newbie" question, which is sort of tied into 
the last question I posted.

I have three modules:
   mailstash
   mailstash_gui.tmainform
   appvars

In appvars, I have the following line:
   global user_preferences   # destined to be a dictionary

In mailstash_gui.tmainform I have
   from appvars import *

I have that same import call at the top of mailstash.  Now then, because 
I'm trying to outmaneuver gnome.ui (imported into 
mailstash_gui.tmainform) so I can parse my command line before Gnome is 
initialized, I actually have most of the application initialization code 
*before* I make the call to import mailstash_gui.tmainform.  In this 
code before the import, I define some values for the user_preferences 
dictionary.  Sorry for being either confusing or too precise ... the 
definition of user_preferences comes *after* importing appvars, but 
*before* importing mailstash_gui.tmainform.

Because this variable (I thought) was global, I assumed that since it 
was also included in mailstash_gui.tmainform, the variable would contain 
the same value across both modules.  However, after getting a KeyError, 
I checked and discovered that user_preferences claims to be "undefined" 
in mailstash_gui.tmainform.

Even after re-reading the tutorial and docs, I think that I am grossly 
misunderstanding the scoping rules for Python, or the use of the 
"global" keyword.  Could someone please clear this up for me? 
 Meanwhile, I'm going to go bang my head against the desk, and see if I 
get a revelation ...

Thanks,

Travis Beaty
Mason City, Iowa.





More information about the Python-list mailing list