Reputation: 7303
How to get a list of all methods of a class which have been decorated with the @property
decorator?
Having the following example, the answer should get a list with only x
and y
included, but not z
, since it hasn't been decorated with @property
:
class C(object):
def __init__(self):
self._x = None
self._y = None
@property
def x(self):
"""I'm the 'x' property."""
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
@property
def y(self):
return self._y
def z(self):
pass
Upvotes: 1
Views: 100
Reputation: 26037
Use the following:
k = [name for name, value in vars(C).items() if isinstance(value, property)]
Now, print k
Prints: ['x', 'y']
Upvotes: 2
Reputation: 27333
Methods decorated by @property
are instances of property
:
>>> [name for name in dir(C) if isinstance(getattr(C, name), property)]
['x', 'y']
Upvotes: 6