如何使用变量作为函数名称,以便我可以获取函数列表并在循环中初始化它们。我得到了我期望的错误,这是str对象不可调用的错误。但我不知道如何解决它。谢谢。如何在python中使用变量作为函数名称
#Open protocol configuration file
config = configparser.ConfigParser()
config.read("protocol.config")
# Create new threads for each protocol that is configured
protocols = ["ISO", "CMT", "ASCII"]
threads = []
threadID = 0
for protocol in protocols:
if (config.getboolean(protocol, "configured") == True):
threadID = threadID + 1
function_name = config.get(protocol, "protocol_func")
threads.append(function_name(threadID, config.get(protocol, "port")))
# Start new threads
for thread in threads:
thread.start()
print ("Exiting Main Protocol Manager Thread")
** **这些功能在哪里?在特定的模块?当前模块?通常,将函数作为字典键并进行查找是最干净的 - 例如,对于应该放置在该字典中的公开函数使用装饰器;这种方式的元编程骇客很少。 –