运行类中的所有功能 [英] Run all functions in class
本文介绍了运行类中的所有功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试运行类中的所有函数,而无需单独键入它们。
I am trying to run all the functions in my class without typing them out individually.
class Foo(object):
def __init__(self,a,b):
self.a = a
self.b=b
def bar(self):
print self.a
def foobar(self):
print self.b
我想这样做但要循环执行,因为我的实际班级大约有8-10个函数。
I want to do this but with a loop, because my actual class has about 8-10 functions.
x = Foo('hi','bye')
x.bar()
x.foobar()
推荐答案
这是解决此问题的最简单方法,也可以在其中进行更改。
This is the easiest way to solve this problem, also it's kind of flexible to do changes in it.
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
这篇关于运行类中的所有功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文