Skip to content

Instantly share code, notes, and snippets.

@nicain
Created June 26, 2020 05:09
Show Gist options
  • Save nicain/4f16d7f6c7c7ad5b0db9d5c4421bf1e5 to your computer and use it in GitHub Desktop.
Save nicain/4f16d7f6c7c7ad5b0db9d5c4421bf1e5 to your computer and use it in GitHub Desktop.
Class registry via metaclassing
class MetaRegistry(type):
class_registry = {}
def __new__(meta, name, bases, class_dict):
cls = type.__new__(meta, name, bases, class_dict)
if name not in meta.class_registry:
meta.register_class(meta.class_registry, cls)
return cls
@staticmethod
def register_class(registry, target_class):
registry[target_class.__name__] = target_class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment