Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 继承和循环导入问题_Python - Fatal编程技术网

Python 继承和循环导入问题

Python 继承和循环导入问题,python,Python,我想在Python中实现一个类,该类在执行时返回另一个类的实例 from Bmodule import B class A: def __new__(cls): return B 在模块B模块中: from Amodule import A class B(A): def __init__(self): print("Class B was built") 但这给了我一个导入错误,可能是由于循环导入。我知道我可以将这两个类放在一个唯一的模块中,

我想在Python中实现一个类,该类在执行时返回另一个类的实例

from Bmodule import B
class A:
    def __new__(cls):
        return B
在模块B模块中:

from Amodule import A
class B(A):
    def __init__(self):
        print("Class B was built")
但这给了我一个导入错误,可能是由于循环导入。我知道我可以将这两个类放在一个唯一的模块中,但我更喜欢将它们放在单独的模块中。我怎样才能修好它

谢谢

类似这样:

class A:
    def __new__(cls):
        from Bmodule import B
        return super().__new__(B)

class B(A):
    def __init__(self):
        print("Class B was built")

print(A())

不仅导入是循环的:您的类定义本身也是循环的。这无法工作…可能重复的可能重复的