循环Python / IronPython对象方法
作者:互联网
循环Python对象的方法并调用它们的正确方法是什么?
鉴于对象:
class SomeTest():
def something1(self):
print "something 1"
def something2(self):
print "something 2"
解决方法:
您可以使用inspect模块获取类(或实例)成员:
>>> class C(object):
... a = 'blah'
... def b(self):
... pass
...
...
>>> c = C()
>>> inspect.getmembers(c, inspect.ismethod)
[('b', <bound method C.b of <__main__.C object at 0x100498250>>)]
getmembers()返回一个元组列表,其中每个元组都是(name,member). getmembers()的第二个参数是谓词,它过滤返回列表(在这种情况下,只返回方法对象)
标签:python,reflection,introspection,ironpython,python-datamodel 来源: https://codeday.me/bug/20190722/1499286.html