简体   繁体   中英

Passing variable from one class instance to another using Python?

I'm having trouble passing a variable defined in one class instance to another class instance. I am relatively new to using classes, but it is my understanding that variables can be passed from one instance to another by simply defining as part of the class instance (such as in the following example). While I've used this model in the past I've never attempted to do this using a GUI framework such as wxPython .

class Foo(object):
    def __init__(self, var):
        self.var = var

class Bar(object):
    def do_something(self, var):
        print var*3

if __name__ == '__main__':
    f = Foo(3)
    b = Bar()
    b.do_something(f.var)

The problem I'm having is that the wxPython instance seems to be predefined and will not accept any additional parameters (allowing me to only pass things like title, size, etc.) to the class instance.

The other issue I'm facing is I'm attempting to pass the variable three classes deep by calling a dialog window, and from the dialog I call a separate class designed to start a worker thread.

So my questions are:

  1. How can I pass the variable from the first class instance to the third class instance?

  2. How can I override the wxPython instance to allow for the definition of additional variables?

  3. OR, Could it be possible to create a custom event handler to pass the necessary data?

To clarify...

I'm using Python and would like to think that I understand the fundamentals of programming using Classes and a GUI with frameworks such as Tkinter and wxPython (used in this project). I've written a main class/instance that gets some data from the user and I would like to be able to pass the information stored in self.main_instance_var and pass it along to a second class/instance (in this case a Progress Dialog window called from the first class.

When I attempted to use the above model in my Progress Dialog I got a very uninformative syntax error ('non-keyword arg after keyword arg'). Preventing me from further passing the variable from the Progress Dialog window on to the worker thread. If I had gotten an exception that would have been one thing but the syntax error I don't understand. Look below for a short example:

class ProgressDialog(wx.Dialog):

    def __init__(self, parent, title, myVar):     # Generates syntax error on this line
        super(ProgressDialog, self).__init__(parent=parent, 
            title=title, size=(500, 110))
        self.var = myVar

Basic Source (by request, sorry its so dirty):

import time
import os, sys, wx
from ftplib import FTP_TLS

from threading import Thread
from wx.lib.pubsub import Publisher

########################################################################
class FtpValues(object):
    """ Returns a property attribute - called by FtpFileTransfer
    Used to set values/variables for Host, USERID, PASSWD, FILE """

    #----------------------------------------------------------------------
    def __init__(self):
        self.varList = None

    #----------------------------------------------------------------------    
    def GetValues(self):
        return self.varList

    #----------------------------------------------------------------------
    def SetValues(self, HOST, USERID, PASSWD, FILE):
        self.varList = [HOST, USERID, PASSWD, FILE]

    #----------------------------------------------------------------------
    def DelValues(self):
        del self.valList

    Values = property(GetValues, SetValues, DelValues, "Set/Get FtpValues")

    # http://docs.python.org/library/functions.html#property

########################################################################
class FtpFileTransfer(Thread):
    """Test Worker Thread Class."""

    #----------------------------------------------------------------------
    def __init__(self):
        """Init Worker Thread Class."""
        Thread.__init__(self)
        self.StartTransfer()        # start the thread

    #----------------------------------------------------------------------
    def StartTransfer(self):        # was named run - started automatically                                    
        """Run Worker Thread."""    # when called by the start method
        # This is the code executing in the new thread.

        HOST, USERID, PASSWD, FILE = FtpValues.Values
        BLOCKSIZE = 57344
        try:
            ftp = FTP_TLS(HOST)
            ftp.login(USERID, PASSWD)
            ftp.prot_p()
            ftp.voidcmd("TYPE I")
            f = open(FILE, 'rb')
            datasock, esize = ftp.ntransfercmd(
                    'STOR %s' % os.path.basename(FILE))
            size = os.stat(FILE)[6]
            bytes_so_far = 0
            while 1:
                buf = f.read(BLOCKSIZE)
                if not buf:
                    break
                datasock.sendall(buf)
                bytes_so_far += len(buf)
                msg = [bytes_so_far, size]
                Publisher().sendMessage("update", msg)
        except: raise
        finally:
            try:
                datasock.close()
                f.close()
                ftp.voidresp()
                ftp.quit()
                print 'Complete...'
            except: pass

        wx.CallAfter(Publisher().sendMessage, "update", "Database Transfer Complete!")


########################################################################
class ProgressDialog(wx.Dialog):

    def __init__(self, parent, title):
        super(ProgressDialog, self).__init__(parent=parent, 
            title=title, size=(500, 110))

        self.displayLbl = wx.StaticText(self, -1, 'Verifying Database Files... ', (20, 20)) #Preparing for Transfer...
        self.gauge = wx.Gauge(self, -1, 100, (20, 45), (370, 24))        
        self.btn = btn = wx.Button(self, -1, 'Cancel', (400, 45), (-1, 25))
        btn.Bind(wx.EVT_BUTTON, self.OnClose)

        # listens for response from worker thread
        Publisher().subscribe(self.updateDisplay, "update")

        FtpFileTransfer()#.StartTransfer(HOST, USERID, PASSWD, FILE)        #Start the FTP Worker Thread
        #self.OnStart()

    #----------------------------------------------------------------------
    def run(self):
        FtpFileTransfer(HOST, USERID, PASSWD, FILE)

    #----------------------------------------------------------------------
    def OnClose(self, event):
        """ Place Holder """
        if self.btn.GetLabel() == 'Finish':
            # Do Something!
            pass
        return None

    #----------------------------------------------------------------------
    def updateDisplay(self, msg):
        """ Receives data from thread and updates the display """
        if isinstance(msg.data, list):
            bytes_so_far, size = msg.data
            k = 100 * bytes_so_far / size
            self.displayLbl.SetLabel("Sent %d of %d bytes %.1f%%" % (bytes_so_far, size, 100 * bytes_so_far / size))
            self.gauge.SetValue(k)
        else:
            self.displayLbl.SetLabel("%s" % msg.data)
            #self.btn.Enable()
            self.btn.SetLabel('Finish')


########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)
        self.displayLbl = wx.StaticText(panel, label="Amount of time since thread started goes here")
        self.btn = btn = wx.Button(panel, label="Start Thread")
        self.gauge = wx.Gauge(panel, -1, 100, size=(370, 24))

        btn.Bind(wx.EVT_BUTTON, self.onButton)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.displayLbl, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(self.gauge, 0, wx.ALL|wx.CENTER, 5)
        panel.SetSizer(sizer)

        self.VarData()
        # create a pubsub receiver
        Publisher().subscribe(self.updateDisplay, "update")

    #----------------------------------------------------------------------
    def onButton(self, event):
        """
        Runs the thread
        """

        chgdep = ProgressDialog(None, title='File Transfer. . .')
        chgdep.ShowModal()
        #chgdep.Destroy()

    #----------------------------------------------------------------------
    def updateDisplay(self, msg):
        """
        Receives data from thread and updates the display
        """
        if isinstance(msg.data, list):
            bytes_so_far, size = msg.data
            k = 100 * bytes_so_far / size
            self.displayLbl.SetLabel("Sent %d of %d bytes %.1f%%" % (bytes_so_far, size, 100 * bytes_so_far / size))
            self.gauge.SetValue(k)
        else:
            self.displayLbl.SetLabel("%s" % msg.data)
            self.btn.Enable()

    #----------------------------------------------------------------------
    def VarData(self):
        HOST = '127.0.0.1'
        USERID = 'test'
        PASSWD = 'P@ssw0rd'
        FILE = r'F:\Programming\temp\Test.zip'
        varList = [HOST, USERID, PASSWD, FILE]
        FtpValues.Values = HOST, USERID, PASSWD, FILE

#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyForm().Show()
    app.MainLoop()

Personally, I like using wx.lib.pubsub to pass information between classes. I do it all the time in my applications. You can read about it here: http://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/

If you need to post data from a thread, you will need to use a thread-safe method like wx.CallAfter, wx.CallLater or wx.PostEvent. You can combine these with pubsub by calling your pubsub publisher inside one of the thread-safe methods. I show how to do just that here: http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/

There's also a good article on threads and wxPython on their wiki: http://wiki.wxpython.org/LongRunningTasks

The approaches suggested in the question are the preferred way of doing things in Python. Statements like b.do_something(f.var) and __init__(self, parent, title, myVar) are perfectly good ways to pass information between classes.

It's common when starting out, but my guess here is that you've taken a small syntax error somewhere and are thinking it's implying that you're taking the wrong general approach. Instead, your general approach looks fine, but you just need to figure out what's causing the specific error.

Comments on other answers:

1) Set/get functions work well too, but Python prefers the properties approach. (Personally, I still use set/get methods out of habit, but it's not as Pythonic.)

2) pubsub is great, but it's not a general tool for "passing information between classes", eg , one wouldn't want to use pubsub for i = int("2"). Pubsub is more for cases where one has, for example, two wxFrames that need to communicate a bit of information. There's a reason it's a part of wxPython and not Python.

You may have worked through this months ago, but I just ran into the very same issue with a wxPython dialog, and got it to work by using informing a global within the function:

elusive_variable="" # declare outside class & function

class MyForm(wx.Frame):

    def onOpenFile(self, event):
        global elusive_variable
        # ...other stuff here
        elusive_variable="Oh hi Mark"

if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
print elusive_variable # variable is liberated!!!

There might be some more enlightned way but this works...

Heres an example:

class One:
    param1 = ['a', 'b']

class Two:
    i = One()
    print i.param1

Save the above code in .py file and run it . You should see the output I guess this can be a simple way to exchange variables from one class to another

In OOP if you want to pass data into and out of an object you should define a set/get function in that class so that you can get data from that object as well as set data in that object. So in your case each instance of your object would call the respective get/set function to pass the data back and forth between your objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM