# Every class is an instance of typeclassMyClass:
passprint(f"type(MyClass): {type(MyClass)}")
print(f"type(type): {type(type)}")
print(f"isinstance(MyClass, type): {isinstance(MyClass, type)}")
# The class statement is syntactic sugar for type()# class Foo(Bar): x = 1# is equivalent to:
# Foo = type("Foo", (Bar,), {"x": 1})
# Custom metaclass inherits from typeclassMeta(type):
def__new__(mcs, name, bases, namespace):
cls = super().__new__(mcs, name, bases, namespace)
cls.created_by = "Meta"return cls
classWidget(metaclass=Meta):
passprint(f"type(Widget): {type(Widget)}")
print(f"Widget.created_by: {Widget.created_by}")
print(f"isinstance(Widget, Meta): {isinstance(Widget, Meta)}")
Output
Click "Run" to execute your code
type is the metaclass of all classes. 'class X: pass' is syntactic sugar for 'X = type("X", (), {})'. Custom metaclasses inherit from type to customize class creation.
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?