properties - list @property decorated methods in a python class -
is possible obtain list of @property decorated methods in class? if how?
example:
class myclass(object): @property def foo(self): pass @property def bar(self): pass how obtain ['foo', 'bar'] class?
anything decorated property leaves dedicated object in class namespace. @ __dict__ of class, or use vars() function obtain same, , value instance of property type match:
[name name, value in vars(myclass).items() if isinstance(value, property)] demo:
>>> class myclass(object): ... @property ... def foo(self): ... pass ... @property ... def bar(self): ... pass ... >>> vars(myclass) dict_proxy({'__module__': '__main__', 'bar': <property object @ 0x1006620a8>, '__dict__': <attribute '__dict__' of 'myclass' objects>, 'foo': <property object @ 0x100662050>, '__weakref__': <attribute '__weakref__' of 'myclass' objects>, '__doc__': none}) >>> [name name, value in vars(myclass).items() if isinstance(value, property)] ['bar', 'foo'] note include used property() directly (which decorator does, really), , order of names arbitrary (as dictionaries have no set order).
Comments
Post a Comment