【问题标题】:How to get class from other module by name如何按名称从其他模块获取类
【发布时间】:2022-06-12 18:43:03
【问题描述】:

我想通过名称作为字符串从另一个模块中获取一个类,具有以下结构:

> foldera
  > folderb1
    > folderc
      - __init__.py
      - folderc.py (parent)
      - folderc_childa.py (child)
      - ....py (more child implementations)
  > folderb2
      - __init__.py
      - converter.py
      - ....py

并希望 getattr 的模块 folderc 列出其文件的所有类(例如,包含名为 FolderC、FolderCChildA 等的类的列表)

当前getattr(foldera.folderb1.folderc, 'FolderCChildA') 显示未找到。因为在调试器中foldera.folderb1.folderc 不包含比folderc.py 更多的类作为属性

我尝试过类似How to get class object from class name string in the same module? 的操作,但仍然无法正常工作。有人对我犯的错误有任何建议吗?

【问题讨论】:

  • 你能正常使用import导入这些类吗?

标签: python


猜你喜欢
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
  • 2013-11-06
  • 1970-01-01
  • 1970-01-01
  • 2021-05-19
  • 1970-01-01
  • 2022-01-25
相关资源
最近更新 更多