26 – Notify parent class when subclassing

26 – Notify parent class when subclassing#

The dunder class method __init_subclass__ can be used to notify a class when it’s subclassed. This is effective for some metaprogramming without having to resort to metaclasses.

In this example, the class ParentCls will print a message whenever it is subclassed:

class ParentCls:
    def __init_subclass__(cls, **kwargs):
        print(f"{cls} created with {kwargs = }")

The argument cls will be the subclass, and the keyword arguments kwargs come from the subclass definition:

class ChildCls(ParentCls, example=True):
    pass

When the class ChildCls is created, the parent class automatically prints the following:

<class '__main__.ChildCls'> created with kwargs = {'example': True}