Skip to content

Instantly share code, notes, and snippets.

@nicain
Created June 26, 2020 05:27
Show Gist options
  • Save nicain/5b74e2d63026c134937e304d880b2da2 to your computer and use it in GitHub Desktop.
Save nicain/5b74e2d63026c134937e304d880b2da2 to your computer and use it in GitHub Desktop.
def get_registry_metaclass(registry_name, class_name="MetaRegistry"):
registration_method_name = 'register_class'
def new(meta, name, bases, class_dict):
cls = type.__new__(meta, name, bases, class_dict)
if name not in getattr(meta, registry_name):
getattr(meta, registration_method_name)(getattr(meta, registry_name), cls)
return cls
def register_class(registry, target_class):
registry[target_class.__name__] = target_class
return type(class_name, (type,), {registry_name: {},
"__new__": new,
registration_method_name: staticmethod(register_class)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment