[Tutor] Time to Use a Global Variable?
Kent Johnson
kent37 at tds.net
Thu Feb 9 00:56:02 CET 2006
Rich Shepard wrote:
> In my wxPython application the UI is run out of class MyFrame. The method
> associated with File->Quit is called OnFileQuit. All the code for pysqlite is
> in another class called DBinterface, and that has a method closeDB():
>
> def closeDb(self):
> """
> Closes the database connection explicitly.
> """
> self.cur.close()
> self.con.close()
>
> OnFileQuit looks like this:
>
> def OnFileQuit(self, event):
> closeDb()
> fn.close()
> self.Close()
>
> When I test the application and try to close it, python complains
> File "eikos.py", line 315, in OnFileQuit
> closeDb()
> NameError: global name 'closeDb' is not defined
>
> I've tried changing OnFileQuit to call DBinterface.closeDB(), but that
> doesn't work. Obviously I still haven't completely grokked how to refer in
> one class to a method defined in another class. Of course, I've not
> explicitly created an instance of DBinterface within MyFrame, so that may be
> the problem.
Evidently closeDb is an instance method of some class you don't show
here. Does DBinterface create and maintain a single instance of this
class? or some other code creates an instance?
You need to call closeDb on the DBinterface instance, it is not a
module-level function. You haven't shown enough code for me to know how
the OnFileQuit method might gain access to the DBinterface instance; how
do you access other methods for working with the database?
Kent
More information about the Tutor
mailing list