Skip to content Skip to sidebar Skip to footer

How To List All Class Properties

I have class SomeClass with properties. For example id and name: class SomeClass(object): def __init__(self): self.__id = None self.__name = None def get_i

Solution 1:

property_names=[p for p in dir(SomeClass) if isinstance(getattr(SomeClass,p),property)]

Solution 2:

import inspect

def isprop(v):
  return isinstance(v, property)

propnames = [name for (name, value) in inspect.getmembers(SomeClass, isprop)]

inspect.getmembers gets inherited members as well (and selects members by a predicate, here we coded isprop because it's not among the many predefined ones in module inspect; you could also use a lambda, of course, if you prefer).


Post a Comment for "How To List All Class Properties"