如何获取已经用@property装饰器装饰的类的所有方法的列表?
有了下面的例子,答案应该得到一个只有x和y包含的列表,但不是z,因为它没有用@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
Run Code Online (Sandbox Code Playgroud)
装饰的方法是@property以下实例property:
>>> [name for name in dir(C) if isinstance(getattr(C, name), property)]
['x', 'y']
Run Code Online (Sandbox Code Playgroud)