Back to articles
__init_subclass__ Plugin Registry: No Metaclass Needed

__init_subclass__ Plugin Registry: No Metaclass Needed

via Dev.to PythonTildAlice

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. 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

Continue reading on Dev.to Python

Opens in a new tab

Read Full Article
2 views

Related Articles