Accessing Variable Across Modules

Rich Shepard rshepard at appl-ecosys.com
Fri Nov 3 09:12:12 PST 2006


   Both Robin and Chris wrote in response to one of my questions that
supporting functions should be in modules separate from the main
application. I'm trying to do this, but run into a problem with the scope of
a variable. Let me try to briefly explain.

   A.py contains the class MainFrame() and the entire wxPython UI. The
File->New and File->Open menus call the bound functions, OnFileNew() and
OnFileOpen() in the MainFrame class. These set the filename variable,
'projname,' that is the name of the project's SQLite3 database to be opened
or created. In other words, a.py is the master module that imports all the
other modules.

   One of these other modules is b.py. It holds a single class with the
SQLite3 functions to open, create, and close the database connection. These
functions need the 'projname' variable, but b.py is not within the scope of
projname.

   My understanding of variable scope is that projname is local to
OnFileOpen() and OnFileNew(), global within the class MainFrame(), and
enclosing within the a.py module. But, since it's not a built in variable
it's not visible to b.py. Of course, I cannot import a.py into b.py because
that sets up a circular reference.

   What is the appropriate way to handle this situation? I moved the database
functions back into the MainFrame() class, but then had difficulties
getting a pysqlite2 cursor from another module.

Rich

-- 
Richard B. Shepard, Ph.D.               |    The Environmental Permitting
Applied Ecosystem Services, Inc.(TM)    |            Accelerator
<http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863




More information about the wxpython-users mailing list