如何使用變量作爲函數名稱,以便我可以獲取函數列表並在循環中初始化它們。我得到了我期望的錯誤,這是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")
** **這些功能在哪裏?在特定的模塊?當前模塊?通常,將函數作爲字典鍵並進行查找是最乾淨的 - 例如,對於應該放置在該字典中的公開函數使用裝飾器;這種方式的元編程駭客很少。 –