Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
A metaclass is a class used to create a class. Normal
classes are instances of the type class by default. Metaclasses are
usually subclasses of the type
class, which redefines class creation protocol methods in order to
customize the class creation call issued at the end of a class statement; they typically redefine
the methods __new__ and
__init__ to tap into the class
creation protocol. Metaclasses can also be coded other ways—as
simple functions, for example—but they are responsible for making
and returning an object for the new class.
In Python 3.0 and later, use a keyword argument in the
class header line: class
C(metaclass=M). In Python 2.X, use a class attribute
instead: __metaclass__ =
M. In 3.0, the class header line can also name normal
superclasses (a.k.a. base classes) before the metaclass keyword argument.