python - 如何获取模块中定义但未导入的所有类?

标签 python introspection python-2.7

我已经看到了以下问题,但它并没有完全让我到达我想要的位置:How can I get a list of all classes within current module in Python?

特别是,我不想要导入的类,例如如果我有以下模块:

from my.namespace import MyBaseClass
from somewhere.else import SomeOtherClass

class NewClass(MyBaseClass):
    pass

class AnotherClass(MyBaseClass):
    pass

class YetAnotherClass(MyBaseClass):
    pass

如果我使用 clsmembers = inspect.getmembers(sys.modules[__name__], inspect.isclass) 就像链接问题中接受的答案所暗示的那样,它将返回 MyBaseClassSomeOtherClass 以及本模块中定义的 3 个。

我怎样才能只获得 NewClassAnotherClassYetAnotherClass

最佳答案

检查类的 __module__ 属性以找出它是在哪个模块中定义的。

关于python - 如何获取模块中定义但未导入的所有类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5520580/

相关文章:

python-2.7 - PySpark打印到控制台

python - 使用python从列表中提取Json数据

c - 使用 MinGW 防止在 C(而非 C++)中进行名称重整以进行动态符号搜索

php - 获取用户定义的类和函数的源代码?

Python:跟踪列表中移动的元素

python - 如何在python中锁定一个方法(防止它被覆盖)?

python - 实现 __iter__ 和 __getitem__ 的容器的自定义包装器

python - Bottle + Apache + WSGI + session

python - 异步 : keep multiple coroutines running

python - 是否可以从解释器中运行的 python 代码写入 sys._getframe() 返回的 python 框架对象?