The most derived metaclass is selected from the explicitly specified metaclass (if any) and the metaclasses (i.e. type(cls) ) of all specified base classes. The most derived metaclass is one which is a subtype of all of these candidate metaclasses. If none of the candidate metaclasses meets that criterion, then the class definition will fail with$_{TypeError}$. 

## Preparing the class namespace 

Once the appropriate metaclass has been identified, then the class namespace is prepared. If the metaclass has a __prepare__ attribute, it is called as namespace = metaclass.__prepare__(name, bases, **kwds) (where the additional keyword arguments, if any, come from the class definition). The __prepare__ method should be implemented as a$_{classmethod()}$ . The namespace returned by$_{__prepare__}$ is passed in to __new__ , but when the final class object is created the namespace is copied into a new dict . 

If the metaclass has no$_{__prepare__}$ attribute, then the class namespace is initialised as an empty ordered map- ping. 

See also: 

PEP 3115 - Metaclasses in Python 3000 Introduced the$_{__prepare__}$ namespace hook 

## Executing the class body 

The class body is executed (approximately) as$_{exec(body, globals(), namespace)}$ . The key difference from a normal call to$_{exec()}$ is that lexical scoping allows the class body (including any methods) to reference names from the current and outer scopes when the class definition occurs inside a function. 

However, even when the class definition occurs inside the function, methods defined inside the class still cannot see names defined at the class scope. Class variables must be accessed through the first parameter of instance or class methods, or through the implicit lexically scoped$_{__class__}$ reference described in the next section. 

## Creating the class object 

Once the class namespace has been populated by executing the class body, the class object is created by calling metaclass(name, bases, namespace, **kwds) (the additional keywords passed here are the same as those passed to$_{__prepare__}$ ). 

This class object is the one that will be referenced by the zero-argument form of$_{super()}$ . __class__ is an implicit closure reference created by the compiler if any methods in a class body refer to either$_{__class__}$ or $_{super}$. This allows the zero argument form of$_{super()}$ to correctly identify the class being defined based on lexical scoping, while the class or instance that was used to make the current call is identified based on the first argument passed to the method. 

CPython implementation detail: In CPython 3.6 and later, the$_{__class__}$ cell is passed to the metaclass as a __classcell__ entry in the class namespace. If present, this must be propagated up to the$_{type.__new__}$ call in order for the class to be initialised correctly. Failing to do so will result in a$_{RuntimeError}$in Python 3.8. 

When using the default metaclass$_{type}$, or any metaclass that ultimately calls$_{type.__new__}$ , the following additional customisation steps are invoked after creating the class object: 

• first,$_{type.__new__}$ collects all of the descriptors in the class namespace that define a$_{__set_name__()}$ method; 

• second, all of these$_{__set_name__}$ methods are called with the class being defined and the assigned name of that particular descriptor; 

• finally, the$_{__init_subclass__()}$ hook is called on the immediate parent of the new class in its method resolution order. 

After the class object is created, it is passed to the class decorators included in the class definition (if any) and the resulting object is bound in the local namespace as the defined class. 

