Python 不确定函数,通过字符串匹配调用的方法
·
#!/usr/bin/env python
# encoding: utf-8
'''
@author: morgan lions
@time: 8/10/18 9:57 AM
'''
from __future__ import print_function
def func_day(s):
print ('func_day:', s)
def func_year(s):
print ('func_year:', s)
def func_month(s):
print ('func_month:', s)
if __name__ == '__main__':
string = ['day', 'year', 'month']
for s in string:
func = globals().get('func_{0}'.format(s))
#参数可以传递各种类型,对象,包括函数
if s is 'day':
func('这里是Day函数')
if s is 'year':
func('这里是Year函数')
if s is 'month':
func('这里是Month函数')
还可以通过Python 内置函数 eval 来执行
更多推荐



所有评论(0)