【问题标题】:How do I pass variables between class instances or get the caller?如何在类实例之间传递变量或获取调用者?
【发布时间】:2021-06-30 05:42:39
【问题描述】:
class foo():
  def __init__(self)
    self.var1 = 1

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

f = foo()
b = bar()

在 foo 中,我正在做一些将“var1”设置为 1 的操作 在 bar 中,我想访问 var1 的内容

如何从 bar 的实例 b 中访问 foo 的类实例 f 中的 var1

基本上这些类是不同的 wxframes。因此,例如在一个窗口中,用户可能正在输入输入数据,在第二个窗口中,它使用该输入数据来产生输出。在 C++ 中,我会有一个指向调用者的指针,但我不知道如何在 python 中访问调用者。

【问题讨论】:

  • 另外,我从外部意识到我可以做类似 b.setvar(f.getvar()) 的事情,但我希望能够从内部栏访问变量

标签: python


【解决方案1】:

作为 wxPython 中不同页面访问和编辑相同信息的一般方法,请考虑在 MainFrame(或您所称的任何名称)类中创建 info 类的实例,然后将该实例传递到它创建的任何其他页面.例如:

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

输出是:

initialised
1
3
3

info 仅在证明只有一个实例但 page1 已将 info1 变量更改为 3 并且 page2 已注册该更改时才初始化。

【讨论】:

    【解决方案2】:

    没有人提供一个代码示例来展示在不更改 init 参数的情况下执行此操作的方法。您可以简单地在定义这两个类的外部范围中使用一个变量。但是,如果一个类是在与另一个类不同的源文件中定义的,这将不起作用。

    var1 = None
    class foo():
      def __init__(self)
        self.var1 = var1 = 1
    
    class bar():
      def __init__(self):
        print var1
    
    f = foo()
    b = bar()
    

    【讨论】:

    • 如何维护 foo() 在变量 var1 处给出的值?那么,我可以从类 bar() 中打印值“1”吗?
    【解决方案3】:

    与任何语言相同。

    class Foo(object):
        def __init__(self):
            self.x = 42
    
    class Bar(object):
        def __init__(self, foo):
            print foo.x
    
    a = Foo()
    b = Bar(a)
    

    【讨论】:

    • 有没有办法在不改变 init 参数的情况下做到这一点?
    【解决方案4】:

    或者,您可以有一个公共基类,两个派生类都从该基类继承类变量var1。这样,派生类的所有实例都可以访问该变量。

    【讨论】:

      【解决方案5】:

      类似:

      class foo():
        def __init__(self)
          self.var1 = 1
      
      class bar():
        def __init__(self, foo):
          print foo.var1
      
      f = foo()
      b = bar(foo)
      

      您应该能够在 Python 中传递对象,就像在 c++ 中传递指针一样。

      【讨论】:

      • 有没有办法在不改变 init 参数的情况下做到这一点?
      • @Ime,您拥有与 C++ 相同的所有工具,您将如何在 C++ 中做到这一点?
      • 好吧,我会按照您描述的方式完成。但是,似乎在 python 中我无法拥有重载的构造函数,因此如果我更改 init 可能会遇到其他问题
      • @Ime,有什么问题?您应该是唯一构建框架的人,因此传入 foo 应该不是问题。
      【解决方案6】:

      也许这是在提出这个问题后添加到语言中的......

      全局关键字会有所帮助。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-26
        相关资源
        最近更新 更多