温馨提示×

python怎么按照顺序执行函数

九三
593
2021-01-28 14:02:38
栏目: 编程语言

python怎么按照顺序执行函数

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)

0