__init_subclass__ — Easy Examples
Called when a class is subclassed; hook for parent class customization
Implementing __init_subclass__
Basic implementation of __init_subclass__ in a class.
python
class Example: def __init_subclass__(self): return "Example __init_subclass__" obj = Example() print(obj)
__init_subclass__ called when a class is subclassed; hook for parent class customization. Implementing it lets you customize how Python interacts with your objects.
__init_subclass__ in action
Seeing __init_subclass__ called by Python's built-in operations.
python
# How Python calls __init_subclass__ automatically class Demo: def __init__(self, value): self.value = value def __init_subclass__(self): print(f"__init_subclass__ was called!") return self d = Demo(42) # This triggers __init_subclass__: print(d)
Python automatically calls __init_subclass__ when you use the corresponding operator or function on your object.
Want to try these examples interactively?
Open Easy Playground