python - 获取 Python 类的完全限定名称 (Python 3.3+)

标签 python inspect

如何从模块根目录中获取包含完整路径的类名?对于 Python 3.3 及更高版本?

以下是 Python 代码示例:

class A:
    class B:
        class C:
            def me(self):
                print(self.__module__)
                print(type(self).__name__)
                print(repr(self))

x = A.B.C()
x.me()

此代码在 Python 3.3 上输出我:

__main__
C
<__main__.A.B.C object at 0x0000000002A47278>

因此,Python 在内部知道我的对象是 __main__.A.B.C,但我如何以编程方式获取它?我可以解析 repr(self),但这对我来说听起来像是一个 hack。

最佳答案

您正在寻找__qualname__ (在 Python 3.3 中引入):

class A:
    class B:
        class C:
            def me(self):
                print(self.__module__)
                print(type(self).__name__)
                print(type(self).__qualname__)
                print(repr(self))

关于python - 获取 Python 类的完全限定名称 (Python 3.3+),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37568128/

相关文章:

python - 属性错误: 'float' object has no attribute 'gradient' scipy python

python - 数据预处理 Python

python - 如何为 NI VeriStand 共享库传递带有 ctypes 的数组

带有内置函数的 Python inspect.getargspec

Python inspect.stack 很慢

python - 从框架对象获取文档字符串

Python:将 Quill delta 转换为 HTML

python - 返回多个范围的 numpy 数组

python - 如何检查是否使用检查设置了 python 函数参数的默认值?

node.js - 为什么 Chrome 无法检查 Docker 容器中的 nodejs 代码?