Skip to main content
 首页 » 编程设计

python之如何在python中获取对象的属性

2024年11月01日10zhoujg

<分区>

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'] 

感谢@juanpa.arrivillaga 的评论。 vars(obj) 和 vars(obj.__class__) 是不一样的!