简体   繁体   English

如何在 python 中使用一个变量从一个 class 到另一个变量?

[英]how do I use a variable from one class into another in python?

I am new to Python and I am trying to use a variable from one class in to another class, but onfortunately I dont understand how to do it.我是 Python 的新手,我正在尝试使用从一个 class 到另一个 class 的变量,但幸运的是我不明白该怎么做。

I have written the code below.我已经写了下面的代码。 Inside de World class I have a while loop with inside the loop ther is deltaTime.在de World class里面我有一个while循环,循环里面是deltaTime。 I want to pass deltaTime to the class test where i use deltaTime * 2.我想将 deltaTime 传递给我使用 deltaTime * 2 的 class 测试。

How can I do that?我怎样才能做到这一点?

I have searched around but I didn't understand the explanation.我已经四处寻找,但我不明白解释。

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)

Here is class world这里是 class 世界

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

You're passing the self to Test when you do self.test = Test(self) .当您执行self.test = Test(self)时,您将self传递给Test But you didn't add that as a parameter in the Test.__init__ method.但是您没有将其作为参数添加到Test.__init__方法中。 It should be:它应该是:

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

        self.v += world.deltaTime * 2
        print(v)

But When you call Test(self) , you haven't filled in self.deltaTime yet.但是当你调用Test(self)时,你还没有填写self.deltaTime You should use it from a different method in the Test class, that you call from the while loop.您应该通过从while循环调用的Test class 中的不同方法使用它。

class Test():

    def __init__(self, world):
        self.v = 10
        self.world = world

    def display_time(self):
        self.v += world.deltaTime * 2
        print(self.v)

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

You need to create the Test object in the code:您需要在代码中创建Test object:

t_object = Test(...)

and insert the object to the run function:并将 object 插入到运行 function 中:

def run(self, t_object):

and then each time you want to increment the v value you can just increment the attribute:然后每次你想增加 v 值时,你可以增加属性:

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

so the lines here is not needed:所以这里的行是不需要的:

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

**In fact, it's wrong to do it as you did with these 2 lines.. cus it's not updating the attribute but a new variable name v. **事实上,像你对这两行所做的那样做是错误的。因为它不是更新属性,而是一个新的变量名 v。

** There is also no need to put () after the class name if there is no inheritance. ** 如果没有 inheritance,也不需要在 class 名称之后加上 ()。 in this case, only this way is ok:在这种情况下,只有这种方式是可以的:

class Name:

I think you're talking about class composition .我认为您在谈论class 组合

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

Basically, you create the Test class as an independent object and instantiate it inside the World class.基本上,您将测试 class 创建为独立的 object 并在 World class 中实例化它。 Once you do this, you may use Test's methods (like the use_world_deltatime ) whenever you like.完成此操作后,您可以随时使用 Test 的方法(如use_world_deltatime )。

Does it make sense?是否有意义?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM