class ClassB:
def __init__(self):
self.b = "b"
self.__b = "__b"
@property
def propertyB(self):
return "B"
我知道getattr,hasattr...
可以访问属性。
但是为什么没有iterattr
或listattr
?你知道吗
ClassB
对象的预期结果:
{'propertyB': 'B'}
ClassB
类的预期结果:
['propertyB']
谢谢@胡安帕.阿里维拉加的评论。
vars(obj)
和vars(obj.__class__)
是不同的!你知道吗
要列出python类的属性,可以使用dict
示例
有关更多示例和信息,请参见此链接-https://codesachin.wordpress.com/2016/06/09/the-magic-behind-attribute-access-in-python/
使用内置的^{} 如下所示:
使用列表理解:
相关问题 更多 >
编程相关推荐