python - 如何“键入”随机包裹退货
问题描述
我正在构建一个 API 以允许在我正在处理的应用程序中使用插件。我importlib.import_module
用来导入插件。显然我不知道要提前导入哪些模块。有没有办法在我用来进行导入的方法上将返回类型识别为通用模块?
def import_plugin(plugin_name: str) -> <Some generic module type>:
# conditional tests here...
return importlib.import_module("plugins.{}".format(plugin_name))
解决方案
types.ModuleType
模块的类型在模块中给出types
。
import types
type(types) is types.ModuleType
# => True
推荐阅读
- unicode - `? 的用法 :` 在 Arduino 的二维码生成器中
- amazon-web-services - AWS cli JMESpath 查询标签值
- java - 在 Android 中写入 .csv 文件
- sql - SQL Server 为某些错误代码启用日志记录
- database - Rust 返回 leveldb 数据库实例
- python - 当我们有多个目标时,sklearn 中 LASSO 的目标函数是什么?
- tensorflow - Tensorflow Federated 中的联邦学习,有没有办法在客户端应用早期停止?
- c# - 如何在运行时编译 C# 代码目标 .NET 5?
- c++ - boost::system::error_code 和 boost::system::error_code::value() 有什么区别?
- python - 如何向数据框添加列并将所有行设置为特定值