python - 解决元类冲突

标签 python python-3.x multiple-inheritance metaclass

我需要根据某些条件创建一个使用不同基类的类。在某些类(class)中,我臭名昭著:

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

一个例子是 sqlite3,这是一个你甚至可以在解释器中使用的简短例子:

>>> import sqlite3
>>> x = type('x', (sqlite3,), {})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

最佳答案

不使用jdi所说的配方,可以直接使用:

class M_C(M_A, M_B):
    pass

class C(A, B):
    __metaclass__ = M_C

关于python - 解决元类冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11276037/

相关文章:

C++ 多重虚拟继承与 COM

Python继承结构和参数

python - 使用 Cython 时缺少 numpy 属性

python - cython 多次返回

python - 上传应用程序时出错 Python Google App Engine

python - 如何在Python 3中映射两个不同长度的列表?

python - 如何确定函数的周期

python - 从 Python 中的目录转换所有文件(.jpg 到 .png)

python - 如何批量更新Elasticsearch中的字段?

java - 这是否被认为是多重继承