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

和自甴很熟 提交于 2019-11-26 09:50:13

问题


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

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

f = foo()
b = bar()

In foo, I am doing something that produces \"var1\" being set to 1 In bar, I would like to access the contents of var1

How can I access var1 in the class instance f of foo from within the instance b of bar

Basically these classes are different wxframes. So for example in one window the user may be putting in input data, in the second window, it uses that input data to produce an output. In C++, I would have a pointer to the caller but I dont know how to access the caller in python.


回答1:


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.




回答2:


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()



回答3:


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)



回答4:


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.




回答5:


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++.




回答6:


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()


来源:https://stackoverflow.com/questions/7501706/python-how-do-i-pass-variables-between-class-instances-or-get-the-caller

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