メタクラスと多重継承に関する 2 つの質問があります。1 つ目は、クラスでは TypeError が発生するのに では発生しないのはなぜDerived
ですかDerived2
?
class Metaclass(type): pass
class Klass(object):
__metaclass__ = Metaclass
#class Derived(object, Klass): pass # if I uncomment this, I get a TypeError
class OtherClass(object): pass
class Derived2(OtherClass, Klass): pass # I do not get a TypeError for this
正確なエラー メッセージは次のとおりです。
TypeError: Error when calling the metaclass bases
Cannot create a consistent method resolution order (MRO) for bases object, Klass
super
2 番目の質問は次のとおりです。この場合、なぜ機能しないのですか (__init__
代わりにを使用すると__new__
、super
再び機能します):
class Metaclass(type):
def __new__(self, name, bases, dict_):
return super(Metaclass, self).__new__(name, bases, dict_)
class Klass(object):
__metaclass__ = Metaclass
そこに私は得る:
TypeError: Error when calling the metaclass bases type.__new__(X):
X is not a type object (str)
Python 2.6 を使用しています。