Python中类间的通信

Python中类间的通信,python,python-3.x,class,Python,Python 3.x,Class,我可以从另一个类调用一个类,但反之亦然 从类别A(见下文),我可以调用类别B中的方法B,但从类别B,我无法调用类别A中的方法A1或方法A2 我得到了以下错误: NameError: name 'A' is not defined 这是我的密码: 测试_1.py: from test_2 import * class A(): def __init__(self): self.key = 1 self.call_Method_B = B().Meth

我可以从另一个类调用一个类,但反之亦然

类别A
(见下文),我可以调用
类别B
中的
方法B
,但从
类别B
,我无法调用
类别A
中的
方法A1
方法A2

我得到了以下错误:

NameError: name 'A' is not defined
这是我的密码:

测试_1.py:

from test_2 import *

class A():

    def __init__(self):

        self.key = 1
        self.call_Method_B = B().Method_B(self.key)

    def Method_A1(self):
        print("Method_A1: ok")

    def Method_A2(self):
        print("Method_A2: ok")

if __name__ == '__main__':

    start_A = A()
from test_2 import *

class A():
    def __init__(self):
        self.key = 1
        self.call_Method_B = B().Method_B(self.key, A)

    ...
测试_2.py:

class B():

    def Method_B(self,key):
        self.key = key

        if self.key ==1:
            self.call_Method_A1 = A().Method_A1()
        else:
            self.call_Method_A2 = A().Method_A2()
class B():

    def Method_B(self, key, A):
        ...

导入中有一个循环。尝试添加如下导入:

class B():

    def Method_B(self,key):
        from test_1 import A
        ....
if self.key ==1:
    self.call_Method_A1 = A.Method_A1(self)
else:
    self.call_Method_A2 = A.Method_A2(self)

这将仅在定义测试1后从测试1导入
A

要在脚本之间进行通信,需要将测试1作为模块导入:

from test_1 import * 
并将调用
A
的方式更改为:

class B():

    def Method_B(self,key):
        from test_1 import A
        ....
if self.key ==1:
    self.call_Method_A1 = A.Method_A1(self)
else:
    self.call_Method_A2 = A.Method_A2(self)

调用
方法B

测试_1.py:

from test_2 import *

class A():

    def __init__(self):

        self.key = 1
        self.call_Method_B = B().Method_B(self.key)

    def Method_A1(self):
        print("Method_A1: ok")

    def Method_A2(self):
        print("Method_A2: ok")

if __name__ == '__main__':

    start_A = A()
from test_2 import *

class A():
    def __init__(self):
        self.key = 1
        self.call_Method_B = B().Method_B(self.key, A)

    ...
测试_2.py:

class B():

    def Method_B(self,key):
        self.key = key

        if self.key ==1:
            self.call_Method_A1 = A().Method_A1()
        else:
            self.call_Method_A2 = A().Method_A2()
class B():

    def Method_B(self, key, A):
        ...
更为传统的方式是:

# test_1.py

from test_2 import B

class A():

    def __init__(self):
        self.key = 1
        self.b = B(self)
        self.b.method_b(self.key)

    @staticmethod
    def method_a1():
        print("Method_A1: ok")

    @staticmethod
    def method_a2():
        print("Method_A2: ok")


if __name__ == '__main__':
    start_a = A()



# test_2.py

class B():

    def __init__(self, instance_of_a):
        self.a = instance_of_a

    def method_b(self, key):
        self.key = key

        if self.key == 1:
            self.a.method_a1()
        else:
            self.a.method_a2()

如果可能的话,这种安排最好是避免而不是解决。通过对答案进行以下编辑,效果很好:
self.call\u Method\u A1=A.Method\u A1(self)
@H.Dave啊是的,很抱歉没有仔细阅读测试1。这是必需的,因为方法_A1和方法_A2需要一个位置值。这是self,因为它们是从test_1.py中的类继承的