Python › Metaclasses & Class Customization

What is a metaclass in Python, and what is the default metaclass for all classes?

Beginner

Answer

A metaclass is 'the class of a class' -- it defines how classes themselves are constructed, just as a class defines how instances are constructed. The default metaclass for all classes in Python is `type`; when you write `class Foo: pass`, Python actually calls `type('Foo', (), {})` behind the scenes to create the class object.
class Foo:
    pass

print(type(Foo))       # <class 'type'>
print(type(Foo()))     # <class '__main__.Foo'>
print(isinstance(Foo, type))  # True
Real-world example Understanding that classes are themselves objects (instances of type) explains why you can dynamically create classes at runtime using type() directly, without a class statement.

Common follow-ups: How is type() used both as a function and as the base metaclass?;What's the class hierarchy of type itself?