如何使用模块作为文本?

2024-04-26 22:08:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含模块的字典,现在我想使用模块的字符串值(参见示例)。这是可能的还是我最好再编一本字典?你知道吗

import module1
import module2
import module3

module_dict = {
    'module1': module1
    'module2': module2
}

user_input = input()
module_dict[user_input].function()

#module3.function requires two strings to work, one of the string happens to be the name of the module
module3.function('foo', module_dict[user_input])

这是一个非常简单的版本,请原谅我,如果它看起来混乱。我不能使用内置的str函数,因为它会导致module 'modulename' at 'path',这不是我想要的。你知道吗


Tags: 模块oftheto字符串importinput字典