-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.py
47 lines (34 loc) · 1.08 KB
/
registry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""跟踪一个类的所有子类
"""
class RegistryHolder(type):
REGISTRY = {}
def __new__(cls, name, bases, attrs):
new_cls = type.__new__(cls, name, bases, attrs)
"""
使用类名作为键,但是它可以是任何类参数。
"""
cls.REGISTRY[new_cls.__name__] = new_cls
return new_cls
@classmethod
def get_registry(cls):
return dict(cls.REGISTRY)
class BaseRegisteredClass(metaclass=RegistryHolder):
"""
任何继承BaseRegisteredClass将会包含在字典 `RegistryHolder.REGISTRY`,字典中的键是
类型的名字和相关的值,类自己。
"""
def main():
"""
# 在子类化之前
>>> sorted(RegistryHolder.REGISTRY)
['BaseRegisteredClass']
>>> class ClassRegisteredTree(BaseRegisteredClass):
... def __init__(self, *args, **kwargs):
... pass
# 在子类化之后
>>> sorted(RegistryHolder.REGISTRY)
['BaseRegisteredClass', 'ClassRegisteredTree']
"""
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)