Python 不确定函数,通过字符串匹配调用的方法

版权声明:未经本人同意,禁止转载! https://blog.csdn.net/TianPingXian/article/details/81559406
#!/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 来执行

猜你喜欢

转载自blog.csdn.net/TianPingXian/article/details/81559406