python - 如何继承不同类型的对象
问题描述
我知道发生此异常是因为我试图从多个无法在 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
解决方案
推荐阅读
- java - 为什么 map 被多次调用?
- python - 单元测试每个元素在 2 个列表中匹配
- python - Docker 容器中的 Python 服务器脚本不接收来自主机操作系统的请求
- c# - 如何使用 Visual Studio asp.net 更新 MS Access 数据库中的数据?
- python-3.x - 如何在 Scrapy URL 请求中启用 JavaScript?
- opus - Libopus 编码器 max_data_bytes 字段使用什么值?
- scala - 如何将函数传递给 RDD.map?
- ruby - NoMethodError 但做了'require_relative'
- javascript - Chrome 扩展浏览器操作仅有效一次
- google-bigquery - 使用自定义列名将 Avro 文件加载到具有嵌套记录的 GCS