首页 > 解决方案 > 如何继承不同类型的对象

问题描述

我知道发生此异常是因为我试图从多个无法在 C 级别相互协作的内置类型继承。

但是,如果我定义了一个 NonMandatoryField 运算符,我怎么能绕过它:


class NonMandatoryField(str):
    def __new__(cls, field: str):
        if len(field) == 0:
            raise ValueError('mandatory field not satisfied')
        return super().__new__(cls, field)

和一个 IntField 验证器:

class IntField(int):
    def __new__(cls, field: int):
        if type(field) != int:
            raise ValueError('int value not supplied')
        return super().__new__(cls, field)

我该如何实施:

class NonMandatoryIntField(NonMandatoryField, IntField):
     pass

标签: pythonpython-3.xobject

解决方案


推荐阅读