Aug-26-2018, 07:26 PM
(This post was last modified: Aug-26-2018, 07:56 PM by Gribouillis.)
It would perhaps be better to unpack my function a little. I don't claim it is the best way to do this, but I'm going to explain the following code
import inspect def _get_names(cls): for tp in inspect.getmro(cls): for name, value in vars(tp).items(): if isinstance(value, property): yield name _register = {} def property_names(cls): if cls not in _register: _register[cls] = sorted(set(_get_names(cls))) return _register[cls] def add_property_names(cls): cls.property_names = classmethod(property_names) return cls if __name__ == '__main__': class A: pass class B(A): @property def foo(self): return 0 class C(B): @property def bar(self): return self._bar @bar.setter def bar(self, value): self._bar = value add_property_names(A) a = A() print(a.property_names()) c = C() print(c.property_names()) cc = C() print(cc.property_names())