在Python中,我需要高效且通用地测试类的属性是否是实例方法。调用的输入将是被检查属性的名称(字符串)和对象。
无论属性是否为实例方法,hasattr都返回true。
有什么建议吗?
例如:
class Test(object):
testdata = 123
def testmethod(self):
pass
test = Test()
print ismethod(test, 'testdata') # Should return false
print ismethod(test, 'testmethod') # Should return true
您可以使用
inspect
模块:相关问题 更多 >
编程相关推荐