#!/bin/env python3
def func1():
print("running func1")
def func2():
print("running func2")
def func3():
print("running func3")
func_dict={'1':func1,'2':func2,'3':func3}
choice='2'
if choice in func_dict:
func_dict[choice]()
else:
print('not existed')
利用函数对象写的简易框架
改进一下
#!/bin/env python3
def func1():
print("running func1")
def func2():
print("running func2")
def func3():
print("running func3")
func_dict={'1':[功能1,func1]'2':[功能2,func2],'3':[功能3,func3]}
choice='2'
while True:
for k in func_dict:
print(k,func_dict[k][0])
if choice in func_dict:
func_dict[choice][1]()
else:
print('not existed')