python3中的循环导入

时间:2016-10-04 11:07:30

标签: python-3.x package circular-dependency

我有一个包含多个文件的包。每个文件都包含一个类,并且这些类是相互依赖的。如何在不将所有代码放在一个文件中的情况下避免循环依赖。是否有更好的方法来重构代码或有解决循环导入的方法

这是目录结构

.
|-- Complainant.py
|-- Complaint.py
`-- __init__.py

Complaint.py:

from .Complainant import Complainant

class Complaint(BaseDocument):
    ALL_STATUS = ["waiting","resolved", "rejected"]
    text = TextField()
    timestamp = DateTimeField()
    status = TextField()
    complainant_id = TextField()
    department_ids = ListField(TextField())

    def get_complainant(self):
        db = DBManager.db()
        complainant = Complainant.load(db, self.complainant_id)
        return complainant

Complainant.py

from .Complaint import Complaint

class Complainant(BaseDocument):
    account_type = TextField()
    account_handle = TextField()
    complaint_ids = ListField(TextField())

    def get_complaints(self):
        db = DBManager.db()
        complaints = [Complaint.load(db, i) for i in self.complaint_ids]
        return complaints

初始化的.py

from .Complaint import Complaint
from .Complainant import Complainant

__all__ = [
    Complaint,
    Complainant
]

1 个答案:

答案 0 :(得分:4)

在具有循环依赖关系的系统中,为避免循环导入,通常必须将相互依赖的部分放在同一模块中。

在您的情况下,只有一小部分类ComplaintComplainant是相互依赖的。您可以重构模块以将非相互依赖的部分放入BaseComplaint.pyBaseComplainant.py中的类中,并使用第三个模块来定义子类ComplaintComplainant。< / p>

目录结构:

.
|-- BaseComplainant.py
|-- BaseComplaint.py
|-- ComplaintComplainant.py
`-- __init__.py

BaseComplaint.py:

class BaseComplaint(BaseDocument):
    ALL_STATUS = ["waiting","resolved", "rejected"]
    text = TextField()
    timestamp = DateTimeField()
    status = TextField()
    complainant_id = TextField()
    department_ids = ListField(TextField())

BaseComplainant.py

class BaseComplainant(BaseDocument):
    account_type = TextField()
    account_handle = TextField()
    complaint_ids = ListField(TextField())

ComplaintComplainant.py(也许你可以找到一个更好的名字)

from .BaseComplaint import BaseComplaint
from .BaseComplainant import BaseComplainant

class Complaint(BaseComplaint):
    def get_complainant(self):
        db = DBManager.db()
        complainant = Complainant.load(db, self.complainant_id)
        return complainant

class Complainant(BaseComplainant):
    def get_complaints(self):
        db = DBManager.db()
        complaints = [Complaint.load(db, i) for i in self.complaint_ids]
        return complaints

init.py

from .ComplaintComplainant import Complaint, Complainant

__all__ = [
    Complaint,
    Complainant
]