通过字符串调用函数
def chinese(text): print("jieba分词") def english(text): print("nltk处理对文本进行分词") str_func_pair = {"chinese": chinese, 'english': english} def textprocess(file, language): text = open(file).read() # 字典调用函数名 str_func_pair[language](text) file = '1.txt' language = 'english' textprocess(file, language)
globals()或者eval
def chinese(text): print("jieba分词") def english(text): print("nltk处理对文本进行分词") # 查看全局变量 print(globals()) def textprocess(file, language): text = open(file).read() eval(language)(text) #或者通过globals调用函数名 # globals()[language](text) file = '1.txt' language = 'english' textprocess(file, language)