x = Foo('hi','bye')
public_method_names = [method for method in dir(x) if callable(getattr(x, method)) if not method.startswith('_')] # 'private' methods start from _
for method in public_method_names:
getattr(x, method)() # call
import threading
from threading import Thread
class ClassName():
def func1(self):
print ('2')
def func2(self):
print ('3')
def runall(self):
if __name__ == '__main__':
Thread(target = self.func1).start()
Thread(target = self.func2).start()
run = ClassName()
run.runall() # will run all the def's in the same time
可以使用} 来判断哪些是函数。只要调用任何函数。
dir()
或__dict__
遍历对象的所有属性。您可以使用isinstance()
和^{更新
正如Tadhg所评论的,
inspect.ismethod
似乎是最好的选择。下面是一些示例代码:您可以获取实例的所有“public”方法的列表:
有关getattr的详细信息 实际上,Python没有
public
或private
语义,如果感兴趣,可以读取that这是解决这个问题的最简单的方法,而且在里面做一些改变也是很灵活的。
相关问题 更多 >
编程相关推荐