查询属性名称及其 class
Query attribute name together with its class
可以用__name__
询问属性的名称。不过,我也想查询一下class的名字:
class Test:
__slots__ = ['a', 'b']
print(Test.a.__name__) # → 'a' but wanted 'Test.a' or only the class 'Test'
听起来你想要 __qualname__
。
class Test:
__slots__ = ['a', 'b']
Test.a.__qualname__
# gives 'Test.a'
见https://www.python.org/dev/peps/pep-3155/
Is there also a magic method of getting the class name only?
您可以通过 Test.a.__objclass__
获取 class 本身。所以 class 的名称可以通过
获得
Test.a.__objclass__.__name__
# gives 'Test'
可以用__name__
询问属性的名称。不过,我也想查询一下class的名字:
class Test:
__slots__ = ['a', 'b']
print(Test.a.__name__) # → 'a' but wanted 'Test.a' or only the class 'Test'
听起来你想要 __qualname__
。
class Test:
__slots__ = ['a', 'b']
Test.a.__qualname__
# gives 'Test.a'
见https://www.python.org/dev/peps/pep-3155/
Is there also a magic method of getting the class name only?
您可以通过 Test.a.__objclass__
获取 class 本身。所以 class 的名称可以通过
Test.a.__objclass__.__name__
# gives 'Test'