4
class ClassB:
    def __init__(self):
        self.b = "b"
        self.__b = "__b"

    @property
    def propertyB(self):
        return "B"

I know getattr,hasattr... can access the property. But why don't have the iterattr or listattr?

Expect result of ClassB object:

{'propertyB': 'B'}

Expect result of ClassB class:

['propertyB']

Thanks @juanpa.arrivillaga 's comment. vars(obj) and vars(obj.__class__) is different!

JustWe
  • 4,250
  • 3
  • 39
  • 90

2 Answers2

4

Use the built-in vars as follows:

properties = []
for k,v in vars(ClassB).items():
    if type(v) is property:
        properties.append(k)

Using a list-comprehension:

>>> [k for k,v in vars(ClassB).items() if type(v) is property]
['propertyB']
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
1

To list propeties of a python Class you can use __dict__

Example

>>> class C(object):
x = 4

>>> c = C()
>>> c.y = 5
>>> c.__dict__
{'y': 5}

See this link for more examples and information - https://codesachin.wordpress.com/2016/06/09/the-magic-behind-attribute-access-in-python/