DEV Community

TildAlice
TildAlice

Posted on • Originally published at tildalice.io

__init_subclass__ Plugin Registry: No Metaclass Needed

Why Your Plugin System Is Probably Overengineered

Metaclasses get a lot of hype in Python. Every "advanced Python" tutorial eventually builds some elaborate class factory with __new__ and __init__ hooks, leaving you with code that nobody (including future-you) can debug. But since Python 3.6, there's been a simpler way to build self-registering plugin systems that most developers overlook: __init_subclass__.

I've written about metaclasses for validation performance gains, and they do have their place. But for the common case of "I want subclasses to automatically register themselves," metaclasses are overkill. __init_subclass__ does the job in about 10 lines of code.

A developer typing code on a laptop with a Python book beside in an office.

Photo by Christina Morillo on Pexels

What init_subclass Actually Does

When Python creates a new class that inherits from your base class, it calls __init_subclass__ on the parent. That's it. No metaclass magic, no type() weirdness, no mysterious __prepare__ hooks.


python
class Plugin:
    _registry = {}


---

*Continue reading the full article on [TildAlice](https://tildalice.io/python-init-subclass-plugin-registry/)*
Enter fullscreen mode Exit fullscreen mode

Top comments (0)