Paul Manta
Paul Manta

Reputation: 31597

Metaclass conflict, multiple inheritance, and instance as parent

I've been messing around with the dark arts of Python and there's something I'd like help understanding. Given a class Foo, here's some ways I attempted to inherit from it:

  1. class A(Foo) — Works, unsurprizingly
  2. class B(Foo()) — works provided Foo has an appropriate __new__ method (which I provided)
  3. class C(Foo(), Foo) — works, under the same conditions as B
  4. class D(Foo, Foo()) — gives the famous metaclass error:

    Traceback (most recent call last):
       File "test.py", line 59, in
            class D(Foo, Foo()):
    TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

What exactly is it that causes this conflict? When I inherit from (Foo(), Foo) (instance first, class second) it works, but when I inherit from (Foo, Foo()) (class first, instance second), it doesn't.

Upvotes: 4

Views: 2944

Answers (1)

Ben
Ben

Reputation: 71590

When you "inherent from an instance", what you're really doing is a strange way of using a metaclass. Normally, class objects are instances of type. In the case of class B above, it inherits from an instance of Foo. This is exactly what would happen if you defined a class with Foo as its metaclass, and then inherited from that.

So my speculation as to what's happening here is that Python is processing the base classes in reverse MRO order.

Class C works because the first parent class to be processed is Foo, whose class is type. This means that the metaclass of D must be type, or some subclass thereof. Then Foo() is processed, whose class is Foo, which is a subclass of type, so everything's fine.

Class D fails because the first parent class to be processed is Foo(), which sets a constraint that D have a metaclass of Foo (or subclass). Then Foo comes along, whose class type is not a subclass of Foo.

That is a complete guess, but you could try and see if the Python documentation on metaclasses requires when you multiply inherit from two classes with different metaclasses, where the metaclasses involved have a subtype relationship, that you put them in a particular order.

Upvotes: 5

Related Questions