you're manually creating the class using the metaclass. And here's the catch:
__init_subclass__
only gets called when you define a class using the class
keyword — not when you manually create it using a metaclass like Meta(...)
.
So yeah, that's why you're not seeing the print()
output , Python just doesn't automatically call __init_subclass__
in this situation.
So what can you do?
If you still want __init_subclass__
to run when creating classes dynamically, you'll have to call it yourself inside your metaclass. Something like the below:
This way, you're mimicking what Python does under the hood when you use the class
statement.
for base in bases:
if hasattr(base, '__init_subclass__'):
base.__init_subclass__(new_cls)