I need to create a class that use 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:
一个例子是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
>>>
How can I solve this issue?
我如何解决这个问题?
Thanks.
谢谢。
5 个解决方案
#1
12
Instead of using the receipe as mentioned by jdi, you can directly use:
不使用jdi提到的receipe,您可以直接使用:
class M_C(M_A, M_B):
pass
class C(A, B):
__metaclass__ = M_C
#2
11
Your example using sqlite3
is invalid because it is a module and not a class. I have also encountered this issue.
使用sqlite3的示例无效,因为它是一个模块而不是一个类。我也遇到过这个问题。
Heres your problem: The base class has a metaclass that is not the same type as the subclass. That is why you get a TypeError
.
您的问题是:基类有一个元类,它的类型与子类不同。这就是为什么你会得到一个类型错误。
I used a variation of this activestate snippet using noconflict.py. The snippet needs to be reworked as it is not python 3.x compatible. Regardless, it should give you a general idea.
我使用noconflict.py对这个activestate片段进行了修改。需要对代码片段进行重新处理,因为它不是python 3。x兼容。不管怎样,它应该给你一个大概的概念。
Problem snippet
问题代码片段
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
Solution snippet
解决方案的片段
from noconflict import classmaker
class C(A,B):
__metaclass__=classmaker()
print C
#<class 'C'>
The code recipe properly resolves the metaclasses for you.
代码配方为您正确地解析元类。
#3
6
To use the pattern described by @michael, but with both Python 2 and 3 compatibility (using the six
library):
使用@michael描述的模式,但同时具有Python 2和3的兼容性(使用6库):
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
#4
1
As far as I understood from the previous answers the only think we usually have to do manually is:
我从之前的回答中了解到,我们通常只需要手工做:
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
But we can automate the last two lines now by:
但我们现在可以自动完成最后两行:
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
Since we do not use any version-specific metaclass syntax this metaclass_resolver
works with Python 2 as well as Python 3.
由于我们不使用任何特定于版本的元类语法,这个元类解析器可以使用Python 2和Python 3。
#5
0
This also happens when you try to inherit from a function and not a class.
当您尝试从函数继承而不是从类继承时也会发生这种情况。
Eg.
如。
def function():
pass
class MyClass(function):
pass
#1
12
Instead of using the receipe as mentioned by jdi, you can directly use:
不使用jdi提到的receipe,您可以直接使用:
class M_C(M_A, M_B):
pass
class C(A, B):
__metaclass__ = M_C
#2
11
Your example using sqlite3
is invalid because it is a module and not a class. I have also encountered this issue.
使用sqlite3的示例无效,因为它是一个模块而不是一个类。我也遇到过这个问题。
Heres your problem: The base class has a metaclass that is not the same type as the subclass. That is why you get a TypeError
.
您的问题是:基类有一个元类,它的类型与子类不同。这就是为什么你会得到一个类型错误。
I used a variation of this activestate snippet using noconflict.py. The snippet needs to be reworked as it is not python 3.x compatible. Regardless, it should give you a general idea.
我使用noconflict.py对这个activestate片段进行了修改。需要对代码片段进行重新处理,因为它不是python 3。x兼容。不管怎样,它应该给你一个大概的概念。
Problem snippet
问题代码片段
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
Solution snippet
解决方案的片段
from noconflict import classmaker
class C(A,B):
__metaclass__=classmaker()
print C
#<class 'C'>
The code recipe properly resolves the metaclasses for you.
代码配方为您正确地解析元类。
#3
6
To use the pattern described by @michael, but with both Python 2 and 3 compatibility (using the six
library):
使用@michael描述的模式,但同时具有Python 2和3的兼容性(使用6库):
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
#4
1
As far as I understood from the previous answers the only think we usually have to do manually is:
我从之前的回答中了解到,我们通常只需要手工做:
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
But we can automate the last two lines now by:
但我们现在可以自动完成最后两行:
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
Since we do not use any version-specific metaclass syntax this metaclass_resolver
works with Python 2 as well as Python 3.
由于我们不使用任何特定于版本的元类语法,这个元类解析器可以使用Python 2和Python 3。
#5
0
This also happens when you try to inherit from a function and not a class.
当您尝试从函数继承而不是从类继承时也会发生这种情况。
Eg.
如。
def function():
pass
class MyClass(function):
pass