python中按文本順序執行函數,具體方法如下:
1.首先,新建文本文件test.txt,并在文件中添加以下內容;
func1,life is short
func2,use python
func1, hello word
2.test文件新建好后,在python中使用main函數定義執行順序;
class Example(object):
def __init__(self):
pass
def func1(self, arg):
print 'this is func1, arg is {}.'.format(arg)
def func2(self, arg):
print 'this is func2, arg is {}.'.format(arg)
if __name__ == '__main__':
example_instance = Example()
with open('test.txt', 'r') as f:
for line in f.readlines():
function_name, args = line.strip().split(',')
getattr(example_instance, function_name)(args)