【问题标题】:how do I use a variable from one class into another in python?如何在 python 中使用一个类中的变量到另一个类中?
【发布时间】:2020-07-06 22:02:37
【问题描述】:

我是 Python 新手,我正在尝试将一个类中的变量用于另一个类,但幸运的是我不明白该怎么做。

我已经写了下面的代码。在 de World 类内部,我有一个 while 循环,在循环内部是 deltaTime。我想将 deltaTime 传递给我使用 deltaTime * 2 的类测试。

我该怎么做?

我找遍了,但我不明白解释。

import time as tm

class Test():
  def __init__(self):
    self.v = 10

    v += 'here I need the deltaTime that is inside the while loop in the world class ''self.deltaTime' * 2
    print(v)

这里是阶级世界

class World():
    def __init__(self):
        self.test = Test(self)

    def run (self):
        while True:                 # Main real-time simulation loop
            # BEGIN mandatory statements
            self.oldTime = self.time
            self.time = tm.time ()  # The only place where the realtime clock is repeatedly queried
            self.deltaTime = self.time - self.oldTime
            # END mandatory statements
            
            # ... other code, using objects that are in the world, like a racetrack and cars
            
            print ('deltaTime ', self.deltaTime)
            self.screen.update ()
            tm.sleep (0.02)         # Needed to free up processor for other tasks like I/O

world = World ()

world.run ()

【问题讨论】:

    标签: python class variables methods


    【解决方案1】:

    当您执行self.test = Test(self) 时,您将self 传递给Test。但是您没有将其作为参数添加到 Test.__init__ 方法中。应该是:

    class Test():
        def __init__(self, world):
            self.v = 10
            self.world = world
    
            self.v += world.deltaTime * 2
            print(v)
    

    但是当您拨打Test(self)时,您还没有填写self.deltaTime。您应该从 Test 类中的不同方法使用它,您从 while 循环中调用。

    class Test():
    
        def __init__(self, world):
            self.v = 10
            self.world = world
    
        def display_time(self):
            self.v += world.deltaTime * 2
            print(self.v)
    

    然后从World.run()调用test.display_time()

    【讨论】:

      【解决方案2】:

      我想你说的是class composition

      import time as tm
      
      class Test():
        def __init__(self):
          self.v = 10
      
        def use_world_deltatime(self, delta_time):
          v += self.deltaTime * 2
          print(v)
      
      class World():
          def __init__(self):
              self.test = Test()
      
          def run (self):
              while True:                 # Main real-time simulation loop
                  # BEGIN mandatory statements
                  self.oldTime = self.time
                  self.time = tm.time ()  # The only place where the realtime clock is repeatedly queried
                  self.deltaTime = self.time - self.oldTime
                  # END mandatory statements
                  
                  # ... other code, using objects that are in the world, like a racetrack and cars
                  self.test.use_world_deltatime(self.deltaTime)
                  print ('deltaTime ', self.deltaTime)
                  self.screen.update ()
                  tm.sleep (0.02)         # Needed to free up processor for other tasks like I/O
      
      world = World()
      
      world.run()
      

      基本上,您将 Test 类创建为一个独立的对象,并在 World 类中实例化它。完成此操作后,您可以随时使用 Test 的方法(如 use_world_deltatime)。

      有意义吗?

      【讨论】:

        【解决方案3】:

        需要在代码中创建Test对象:

        t_object = Test(...)
        

        并将对象插入到运行函数中:

        def run(self, t_object):
        

        然后每次你想增加 v 值时,你可以增加属性:

        t_object.v += self.deltaTime
        print(t_object.v)
        

        所以这里的行是不需要的:

        v += 'here I ne ... 
        print(v)
        

        **事实上,像你对这两行所做的那样做是错误的..因为它不是更新属性而是一个新的变量名v。

        ** 如果没有继承,也不需要在类名后面加上()。在这种情况下,只有这种方式是可以的:

        class Name:
        

        【讨论】:

          猜你喜欢
          • 2014-10-06
          • 1970-01-01
          • 2017-07-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多