Python: How do I pass variables between class instances or get the caller?

前端 未结 6 600
自闭症患者
自闭症患者 2020-12-05 16:12
class foo():
  def __init__(self)
    self.var1 = 1

class bar():
  def __init__(self):
    print \"foo var1\"

f = foo()
b = bar()

In foo, I am do

6条回答
  •  -上瘾入骨i
    2020-12-05 16:48

    As a general way for different pages in wxPython to access and edit the same information consider creating an instance of info class in your MainFrame (or whatever you've called it) class and then passing that instance onto any other pages it creates. For example:

    class info():
        def __init__(self):
            self.info1 = 1
            self.info2 = 'time'
            print 'initialised'
    
    class MainFrame():
        def __init__(self):
            a=info()
            print a.info1
            b=page1(a)
            c=page2(a)
            print a.info1
    
    class page1():
        def __init__(self, information):
            self.info=information
            self.info.info1=3
    
    class page2():
        def __init__(self, information):
            self.info=information
            print self.info.info1
    
    t=MainFrame()
    

    Output is:

    initialised
    1
    3
    3
    

    info is only initialised once proving there is only one instance but page1 has changed the info1 varible to 3 and page2 has registered that change.

提交回复
热议问题