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
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
除了使用由JDI提到receipe的,你可以直接使用:
class M_C(M_A, M_B):
pass
class C(A, B):
__metaclass__ = M_C
您例如使用sqlite3
是无效的,因为它是一个模块,而不是一类。 我也遇到了这个问题。
您的继承人问题:基类有一个元类是不是同一类型的子类。 这就是为什么你会得到一个TypeError
。
我用的变体使用noconflict.py这ActiveState的片段 。 该段需要返工,因为它不是蟒蛇3.X兼容。 无论如何,它应该给你一个总体思路。
问题摘录
class M_A(type):
pass
class M_B(type):
pass
class A(object):
__metaclass__=M_A
class B(object):
__metaclass__=M_B
class C(A,B):
pass
#Traceback (most recent call last):
# File "<stdin>", line 1, in ?
#TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass #of the metaclasses of all its bases
解决方案片断
from noconflict import classmaker
class C(A,B):
__metaclass__=classmaker()
print C
#<class 'C'>
该代码处方正确解析元类为您服务。
使用由@迈克尔描述的图案,但具有既Python 2和3的相容性(使用six
库):
from six import with_metaclass
class M_C(M_A, M_B):
pass
class C(with_metaclass(M_C, A, B)):
# implement your class here
据我从以前的答案,明白了才想起我们通常必须手工做的是:
class M_A(type): pass
class M_B(type): pass
class A(metaclass=M_A): pass
class B(metaclass=M_B): pass
class M_C(M_A, M_B): pass
class C:(A, B, metaclass=M_C): pass
但是,我们可以在最后两行现在自动化:
def metaclass_resolver(*classes):
metaclass = tuple(set(type(cls) for cls in classes))
metaclass = metaclass[0] if len(metaclass)==1 \
else type("_".join(mcls.__name__ for mcls in metaclass), metaclass, {}) # class M_C
return metaclass("_".join(cls.__name__ for cls in classes), classes, {}) # class C
class C(metaclass_resolver(A, B)): pass
由于我们不使用任何特定版本的元类语法这metaclass_resolver
对Python 2以及Python 3中。
当您试图从一个函数,而不是一个类继承也会发生这种情况。
例如。
def function():
pass
class MyClass(function):
pass