How to access properties of Python super classes e.g. via __class__.__dict__?

谁都会走 提交于 2019-11-29 10:23:36

You can use dir():

for attr_name in dir(B):
    attr = getattr(B, attr_name)
    if isinstance(attr, property):
        print attr

You can either use "dir", or you can follow all the classes that are contained in the tuple returned by "mro" (method resolution order,given by the __mro__ attribute on the class) - this later method is the only way of uncovering attributes that where later overriden by subclasses:

>>> class A(object):
...    b = 0
... 
>>> class B(A):
...   b = 1
... 
>>> for cls in B.__mro__:
...     for item in cls.__dict__.items():
...         if item[0][:2] != "__":
...            print cls.__name__, item
... 
B ('b', 1)
A ('b', 0)
>>> 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!