The Python 3 documentation clearly describes how the metaclass of a class is determined:
- if no bases and no explicit metaclas
Regarding your first question the metaclass should be MyMetaclass
(which it's so):
In [7]: print(type(MyClass), type(MyDerived))
The reason is that if the metaclass is not an instance of type python calls the methaclass by passing these arguments to it name, bases, ns, **kwds
(see new_class
) and since you are returning your real metaclass in that function it gets the correct type for metaclass.
And about the second question:
What is the purpose of accepting an arbitrary callable?
There is no special purpose, it's actually the nature of metaclasses which is because that making an instance from a class always calls the metaclass by calling it's __call__
method:
Metaclass.__call__()
Which means that you can pass any callable as your metaclass. So for example if you test it with a nested function the result will still be the same:
In [21]: def metaclass_callable(name, bases, namespace):
def inner():
return MyMetaclass(name, bases, namespace)
return inner()
....:
In [22]: class MyClass(metaclass=metaclass_callable):
pass
....:
In [23]: print(type(MyClass), type(MyDerived))
For more info here is how Python crates a class:
It calls the new_class
function which it calls prepare_class
inside itself, then as you can see inside the prepare_class
python calls the __prepare__
method of the appropriate metaclass, beside of finding the proper meta (using _calculate_meta
function ) and creating the appropriate namespace for the class.
So all in one here is the hierarchy of executing a metacalss's methods:
__prepare__
1__call__
__new__
__init__
And here is the source code:
# Provide a PEP 3115 compliant mechanism for class creation
def new_class(name, bases=(), kwds=None, exec_body=None):
"""Create a class object dynamically using the appropriate metaclass."""
meta, ns, kwds = prepare_class(name, bases, kwds)
if exec_body is not None:
exec_body(ns)
return meta(name, bases, ns, **kwds)
def prepare_class(name, bases=(), kwds=None):
"""Call the __prepare__ method of the appropriate metaclass.
Returns (metaclass, namespace, kwds) as a 3-tuple
*metaclass* is the appropriate metaclass
*namespace* is the prepared class namespace
*kwds* is an updated copy of the passed in kwds argument with any
'metaclass' entry removed. If no kwds argument is passed in, this will
be an empty dict.
"""
if kwds is None:
kwds = {}
else:
kwds = dict(kwds) # Don't alter the provided mapping
if 'metaclass' in kwds:
meta = kwds.pop('metaclass')
else:
if bases:
meta = type(bases[0])
else:
meta = type
if isinstance(meta, type):
# when meta is a type, we first determine the most-derived metaclass
# instead of invoking the initial candidate directly
meta = _calculate_meta(meta, bases)
if hasattr(meta, '__prepare__'):
ns = meta.__prepare__(name, bases, **kwds)
else:
ns = {}
return meta, ns, kwds
def _calculate_meta(meta, bases):
"""Calculate the most derived metaclass."""
winner = meta
for base in bases:
base_meta = type(base)
if issubclass(winner, base_meta):
continue
if issubclass(base_meta, winner):
winner = base_meta
continue
# else:
raise TypeError("metaclass conflict: "
"the metaclass of a derived class "
"must be a (non-strict) subclass "
"of the metaclasses of all its bases")
return winner
1. Note that it get called implicitly inside the new_class function and before the return.