The Python 3 documentation clearly describes how the metaclass of a class is determined:
- if no bases and no explicit metaclas
Well, the type
is of course MyMetaClass
. metaclass_callable
is initially 'selected' as the metaclass since it's been specified in the metaclass kwarg and as such, it's __call__
(a simple function call) is going to be performed.
It just so happens that calling it will print
and then invoke MyMetaClass.__call__
(which calls type.__call__
since __call__
hasn't been overridden for MyMetaClass
). There the assignment of cls.__class__ is made to MyMetaClass
.
metaclass_callable
is called once and then appears to be unrecoverable
Yes, it is only initially invoked and then hands control over to MyMetaClass
. I'm not aware of any class attribute that keeps that information around.
derived classes do not use (as far as I can tell)
metaclass_callable
in any way.
Nope, if no metaclass
is explicitly defined, the best match for the metaclasses of bases (here MyClass
) will be used (resulting in MyMetaClass
).
As for question 2
, pretty sure everything you can do with a callable is also possible by using an instance of type with __call__
overridden accordingly. As to why, you might not want to go full blown class-creation if you simply want to make minor changes when actually creating a class.