![](/img/trans.png)
[英]How in Python can I call a function with a variable which is store as a string
[英]i have a string variable which comes with function names, how do I call the function in Python?
我有一個帶有不同函數名稱的字符串變量,並且我有一個文件,其中包含一組經常與字符串內容匹配的函數,我該如何在Python中調用該函數?
例:
在文件1中
def function1: ...
def function2: ...
def function3: ...
在文件2中
functionname = "function2"
我需要從此文件的File1中調用function2
myfunction = getattr(mymodule, functionname)
myfunction()
eval("function2")()
getattr(<module>, fname)()
name = 'function2'
assert re.match('^(?i)[_a-z][_a-z0-9]*$', name)
eval(name)()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.