2024-06-26 14:29:02 发布
网友
在Python中有没有在循环中使用方法的方法?如下所示:
obj=SomePythonObject() list_of_methods=dir(obj) for i in list_of_methods: try: print obj.i() except: print i,'failed'
是的,这是可能的,使用callable和getattr:
obj=SomePythonObject() list_of_methods=dir(obj) for i in list_of_methods: try: item = getattr(obj, i, None) if callable(item): item() except: print i,'failed'
是的,这是可能的,使用callable和getattr:
相关问题 更多 >
编程相关推荐