假设有一个与所定义的@property
一类:如何检查一个变量是一个属性
class MyClass:
...
@property
def this_is_a_property(self):
return self.some_thing
...
def this_is_a_function(self, x):
...
return other_thing
通常,检查属性是否是一个函数,我可以使用isfunction
从inspect
模块。
import inspect
if inspect.isfunction(MyClass.__dict__['this_is_a_function']):
print('this_is_a_function',' is a function')
我如何检查property
?似乎没有inspect.isproperty
函数。
肯定的,虽然这不是一定的方式来测试此;实际的实例可能是*另一个*描述符,当在一个类上查找时,它只返回一个'property';)(很远是的) – 2015-04-05 19:48:50