解决元类冲突 [英] Resolving metaclass conflicts

查看:57
本文介绍了解决元类冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个根据某些条件使用不同基类的类.在一些课程中,我声名狼藉:

I need to create a class that uses a different base class depending on some condition. With some classes I get the infamous:

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

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

One example is sqlite3, here is a short example you can even use in the interpreter:

>>> 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

推荐答案

您可以直接使用:

class M_C(M_A, M_B):
    pass

class C(A, B):
    __metaclass__ = M_C

这篇关于解决元类冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆