Skip to content

Instantly share code, notes, and snippets.

@multun
Created April 18, 2020 07:48
Show Gist options
  • Save multun/40a67f9eb97bf6ccc7f173ecdc2facfe to your computer and use it in GitHub Desktop.
Save multun/40a67f9eb97bf6ccc7f173ecdc2facfe to your computer and use it in GitHub Desktop.
A Mixin class for making class based plugins
#!/usr/bin/env python3
class PluginSubclassesMixin:
def __init_subclass__(cls, register_base=False, **kwargs):
super().__init_subclass__(**kwargs)
if PluginSubclassesMixin not in cls.__bases__:
return
cls.subclasses = []
if register_base:
cls.subclasses.append(cls)
@classmethod
def mixin_subclass_init(subcls, **kwargs):
super(cls, subcls).__init_subclass__(**kwargs)
cls.subclasses.append(subcls)
cls.__init_subclass__ = mixin_subclass_init
class Stuffinators(PluginSubclassesMixin, register_base=True):
pass
class Stuffluff(Stuffinators):
pass
class Stufflargh(Stuffluff):
pass
print("Stuffinators", Stuffinators.subclasses)
print("Stuffluff", Stuffluff.subclasses)
print("Stufflargh", Stufflargh.subclasses)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment