2个python类相互调用

2个python类相互调用,python,class,callback,Python,Class,Callback,我有两个python类,然后我想依次调用彼此。我想知道你对怎么做有什么建议。我举一个小例子来说明这一点。它正在运行,但我想知道您是否知道针对此类问题的更通用的解决方案 import numpy as np import time np.random.seed(0) class A(): def __init__(self, a0): self.a0 = a0 def initialization(self): print('

我有两个python类,然后我想依次调用彼此。我想知道你对怎么做有什么建议。我举一个小例子来说明这一点。它正在运行,但我想知道您是否知道针对此类问题的更通用的解决方案

import numpy as np 
import time

np.random.seed(0)

class A():
    def __init__(self, a0):
        self.a0 = a0
        
    def initialization(self):
        print('Start A')
        self.b = B(10)
        self.b.start()
    def run(self):
        print('Run A')
        while self.b.flag == 0:
            self.b.runner()
        print('End Run A')
        return self.b.output
     
class B():
    def __init__(self, b0):
        self.b0 = b0
        self.output = None
        self.flag = 0
        
    def start(self):  
        print('Start B')
    def runner(self):
        print('Run B')
        output = self.b0 + np.random.randint(10, 20)
        t = np.random.randint(0, 3)
        time.sleep(t)
        self.output = output
        self.flag = np.random.randint(0,2)
        print('End B')
           
a = A(2)
a.initialization()
a.run()

这个问题对于论坛来说可能有点过于笼统。措辞过于夸张,好的问题清楚地表明了你想要达到的目标。这是一种Python元问题,而不是关于如何做一件事的问题。