【发布时间】:2020-12-15 07:58:20
【问题描述】:
我有一个类拆分成 mixins:
class MyObject(MyObjectFilesMixin, MyObjectProcessingMixin, ...):
def __init__(self, value):
self.value = self.preprocess(value)
mixin 看起来像这样:
class MyObjectFilesMixin:
def load_from_file(cls, filename):
return ...
现在我想在类和 mixins 中添加类型:
class MyObjectFilesMixin:
def load_from_file(cls, filename: str) -> MyObject:
return ...
class MyObjectProcessingMixin:
def preprocess(self: MyObject, value: bytes):
return value # logic is omitted
def append(self: MyObject, other: MyObject):
self.value += other.value
但它会导致循环链接。当然我可以创建一些MyObjectBase(遵循依赖倒置原则),这样MyObject也会继承这个类,mixins会使用它作为参数/返回类型,但这无论如何都会导致错误的类型。可以修复吗??
我错过了很多来自 C++ 的头文件和源文件
【问题讨论】:
标签: python include mixins type-hinting python-typing