TypeErrors Verwendung von Metaklassen in Verbindung mit Mehrfachvererbung

Ich habe zwei Fragen converning Metaklassen und mehrfache Vererbung. Die erste ist: Warum bekomme ich eine TypeError für die Klasse Derived aber nicht für 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

Die genaue Fehlermeldung ist:

TypeError: Error when calling the metaclass bases
Cannot create a consistent method resolution order (MRO) for bases object, Klass

Die zweite Frage ist: Warum tut super Arbeit nicht in diesem Fall(wenn ich __init__ statt __new__, super wieder funktioniert):

class Metaclass(type):
    def __new__(self, name, bases, dict_):
        return super(Metaclass, self).__new__(name, bases, dict_)

class Klass(object):
    __metaclass__  = Metaclass

Dort bekomme ich:

TypeError: Error when calling the metaclass bases type.__new__(X):
X is not a type object (str)

Ich bin mit Python 2.6.

InformationsquelleAutor nils | 2010-02-04
Schreibe einen Kommentar