Skip to content

Instantly share code, notes, and snippets.

@BradWhittington
Created January 12, 2012 10:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BradWhittington/1599710 to your computer and use it in GitHub Desktop.
Save BradWhittington/1599710 to your computer and use it in GitHub Desktop.
Plugin pattern for python classes
plugins = {}
def get_input_plugins():
return plugins['input'].items()
class Plugin(object):
plugin_class = None
@classmethod
def register(cls, name):
plugins[cls.plugin_class][name] = cls
class InputPlugin(Plugin):
plugin_class = 'input'
def process_input(self, something):
raise NotImplementedError
class ExamplePlugin(InputPlugin):
def process_input(self, something):
return str(something)
ExamplePlugin.register('example')
@ailjushkin
Copy link

ExamplePlugin.register('example')

Why not just use cls.__name__ as name argument? Then we just call ExamplePlugin.register()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment