如何继承不同类型的对象

时间:2019-07-23 19:39:40

标签: python python-3.x object

我知道发生此异常是因为我试图从无法在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

0 个答案:

没有答案