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

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 01:53:00

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.

No one has provided a code example showing a way to do this without changing the init arguments. You could simply use a variable in the outer scope that defines the two classes. This won't work if one class is defined in a separate source file from the other however.

var1 = None
class foo():
  def __init__(self)
    self.var1 = var1 = 1

class bar():
  def __init__(self):
    print var1

f = foo()
b = bar()

Same as in any language.

class Foo(object):
    def __init__(self):
        self.x = 42

class Bar(object):
    def __init__(self, foo):
        print foo.x

a = Foo()
b = Bar(a)

Alternatively you could have a common base class from which both derived classes inherit the class variable var1. This way all instances of derived classes can have access to the variable.

Something like:

class foo():
  def __init__(self)
    self.var1 = 1

class bar():
  def __init__(self, foo):
    print foo.var1

f = foo()
b = bar(foo)

You should be able to pass around objects in Python just like you pass around pointers in c++.

Perhaps this was added to the language since this question was asked...

The global keyword will help.

x = 5

class Foo():
    def foo_func(self):
        global x    # try commenting this out.  that would mean foo_func()
                    # is creating its own x variable and assigning it a
                    # value of 3 instead of changing the value of global x
        x = 3

class Bar():
    def bar_func(self):
        print(x)

def run():
    bar = Bar()     # create instance of Bar and call its
    bar.bar_func()  # function that will print the current value of x

    foo = Foo()     # init Foo class and call its function
    foo.foo_func()  # which will add 3 to the global x variable

    bar.bar_func()  # call Bar's function again confirming the global
                    # x variable was changed 


if __name__ == '__main__':
    run()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!