【发布时间】:2020-10-20 14:13:33
【问题描述】:
我为我的类创建了一个自动评分器,它使用命令行参数调用该问题集的关联函数。我需要将命令行参数提取为字符串,然后将其用作函数调用。在示例中,我将字符串分配给 pset,然后调用 pset 并将 studentFile 作为参数传递。问题在于解释器将其视为字符串而不是标识符。
if len(sys.argv) == 2:
try:
# find and store the file
studentFile = helpers.findInSubdirectory(sys.argv[1])
for i in studentFile.split('/'):
if i.endswith('.py'):
pset = i[:-3]
pset(studentFile)
【问题讨论】:
-
你可以从 globals()[pset](studentFile) 获取你的函数
-
这种事情通常是用Python中的字典来完成的。
标签: python string function identifier