registry-pattern

Registry pattern with __init_subclass__ and sub-classable registry

本秂侑毒 提交于 2020-02-05 03:17:06
问题 I want to create a settings registry. I also want to be able to group settings in macro-categories. The following simplified example works with a single registry: class RegistryHolder: registry = {} def __init_subclass__(cls, setting_name=None, **kwargs): super().__init_subclass__(**kwargs) cls.registry[setting_name] = cls class SettingOne(RegistryHolder, setting_name='SettingOne'): pass class SettingTwo(RegistryHolder, setting_name='SettingTwo'): pass And the result is: print(RegistryHolder