Python › Metaclasses & Class Customization
What is a metaclass in Python, and what is the default metaclass for all classes?
BeginnerAnswer
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)) # TrueReal-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.