我有一个包含模块的字典,现在我想使用模块的字符串值(参见示例)。这是可能的还是我最好再编一本字典?你知道吗
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'
,这不是我想要的。你知道吗
您可以从模块的
__name__
属性获取模块名称:输出
相关问题 更多 >
编程相关推荐