python - Python同时继承abc.ABC和typing.SupportsBytes
问题描述
我是编写类型化 Python 代码的新手,但我有其他语言的 OOP 经验。我想创建一个抽象类,也可以使用bytes
函数进行序列化。我正确实现__bytes__
了方法,但我想明确声明此方法可用。
我的类声明如下所示:
from abc import ABC, abstractmethod
from typing import SupportsBytes
class cls(ABC, SupportsBytes):
Python 抱怨TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
.
有什么办法可以改变我的声明以使其工作而不会放弃祖先abc.ABC
或typing.SupportsBytes
祖先?
我曾尝试替换abc.ABC
withabc.ABCMeta
以及调用register
函数 on cls
,但到目前为止,每次尝试都失败了,并出现了自己的错误。
编辑:我使用 Python 3.7.7
解决方案
推荐阅读
- python - 在具有可变文件数的 bash 脚本中使用 pygount
- terraform - 使用 terraform paln 时出现 Terraform 版本问题
- reactjs - 分叉的 NPM 包在生产中不起作用
- python - 使用 python-vlc 播放文件后,VLC 播放器窗口消失
- c# - 找到最近坐标的最快方法
- java - HtppServletRequest getParameterValues 返回 Null
- pyspark - 在 pyspark 中将 Dataframe 输出到 .csv
- java - (Permission denied)] 将文件上传到 Amazon S3 时发生
- pytorch - 如何手动将具有自定义层逻辑的 Caffe 网络转换为 PyTorch?
- python - Replit 上 Python 中的重复延迟问题