Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Declare global var. in python?

Status
Not open for further replies.

j2ecoder

Programmer
Jan 2, 2006
5
PH
Hello,

How can I declare a global variable to be accessible to all of my object methods?
Or is there a java like public static variable?

The type of variable would be a handle to a database connection. Which I will be reusing for all my entire form (wxFrame).

Thanks for any help.
 
If you save your connection as an attribute of your wx.Frame instance then you could use it from within your frame.
Code:
class MyFrame(wx.Frame):
    def __init__(self, conn, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        self.conn = conn

    def your_method(self):
        cursor = self.conn.cursor() # attribute
        # etc.

def main():
    conn = MySQLdb.connect()
    myframe = MyFrame(conn)
    myframe.your_method()
    # etc.

If you must, then you could also have it as a module-level variable.
Code:
myconn = MySQLdb.connect()

class MyFrame(wx.Frame):
    def your_method(self):
        cursor = myconn.cursor() # module-level var
        # etc.

def main():
    myframe = MyFrame()
    myframe.your_method()
    # etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top