[Python-ideas] A way out of Meta-hell (was: A (meta)class algebra) (original) (raw)

Martin Teichmann lkb.teichmann at gmail.com
Sun Feb 15 23:30:40 CET 2015


Hi again,

staring at the code for hours, I just realized that there is a very simple yet powerful solution to the metaclass merging problem. The changes to be made are so simple there isn't even a need to change the documentation!

The current algorithm to find a proper metaclass looks for a class all other metaclasses are a subtype of. For that it uses PyType_IsSubtype. We could simply change that to PyObject_IsSubclass. This would give a great hook into the system: we just need to intercept the subclasshook, and we have a hook into the metaclass algorithm!

Backwards compatibility should not be a problem: how many metaclasses are out there whose metaclass (so the meta-meta-class) overwrites the subclasshook?

I changed the code appropriately, and also added a library that uses this hook. It defines a class (called Dominant for technical reasons, I'm waiting for suggestions for a better name), which acts as a baseclass for metaclasses. All metaclasses inheriting from this baseclass are combined automatically (the algorithm doing that could still be improved, but it works).

While I was already at it, I also added my variant of PEP 422, which fits already into this metaclass scheme.

The code is at https://github.com/tecki/cpython/commits/metaclass-issubclass

Greetings

Martin



More information about the Python-ideas mailing list