Created
June 26, 2020 05:09
-
-
Save nicain/4f16d7f6c7c7ad5b0db9d5c4421bf1e5 to your computer and use it in GitHub Desktop.
Class registry via metaclassing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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