Cross是在Python中实现的吗?

时间:2017-07-09 14:04:15

标签: python oop

如果我想在A类的实现中使用isinstance(var, B),并在B类的实现中使用isinstance(var, A),我该如何正确导入模块?

例如,如果我要实现a+bb+atype(a)Atype(b)B

_A.py

from mypackage._B import B
from mypackage._C import C

class A:
    def __add__(var):
        if isinstance(var, B):
            # Do something
        elif isinstance(var, C):
            # Do something
        # Do some other things

_B.py

from mypackage._A import A
from mypackage._C import C

class B:
    def __add__(var):
        if isinstance(var, A):
            # Do something
        elif isinstance(var, C):
            # Do something
        # Do some other things

_C.py

from mypackage._A import A
from mypackage._B import B

class C:
    def __add__(var):
        if isinstance(var, A):
            # Do something
        elif isinstance(var, B):
            # Do something
        # Do some other things

但是,Python中禁止交叉导入。

我可以通过其他方法避免此限制吗?

编辑:当然,在__init__.py

from ._A import A
from ._B import B
from ._C import C

0 个答案:

没有答案