Python 元类与文档编写全解析
1. Python 元类相关知识
1.1 插件加载系统
在 Python 中,有时会涉及插件加载系统。代码示例如下:
if isinstance(cls.name, str):
metaclass.plugins[cls.name] = cls
return cls
@classmethod
def get(cls, name):
return cls.plugins[name]
@classmethod
def load_directory(cls, module, directory):
for file_ in os.listdir(directory):
name, ext = os.path.splitext(file_)
full_path = os.path.join(directory, file_)
import_path = [module]
if os.path.isdir(full_path):
import_path.append(file_)
elif ext == '.py' and MODULE_NAME_RE.match(name):
import_path.append(name)
else:
# Ignoring non-matching files/directories
continue
plugin =
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



