如何从模块根目录中获取包含完整路径的类名?对于 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/