简体   繁体   English

在 Python 中,如何使用列表中的字符串调用同名函数?

[英]How do you use a string from a list to call a function with the same name, in Python?

I wasn't able to find a satisfying answer anywhere so I decided to ask.我在任何地方都找不到令人满意的答案,所以我决定问一下。

Let's say we have a global counter and a global list假设我们有一个全局计数器和一个全局列表

counter=0
list=["function1","function2",..."functionN"]

we also we have those functions defined:我们还定义了这些函数:

def function1():
  pass
def function2():
  pass
.
.
.
def functionN():
  pass


I have an interface with a button, every time I press it, the global counter increments.我有一个带按钮的界面,每次按下它,全局计数器都会增加。 Depending on the number, I need to call a different function.根据数量,我需要调用不同的函数。 I can implement this with if and elif but I don't think it's that smart.我可以用 if 和 elif 来实现这个,但我认为它不是那么聪明。 Is there a way I can call those functions using the list?有没有办法可以使用列表调用这些函数?

Example例子

when counter=0=>list[0]=>the string is 'function1'=> call function1()当 counter=0=>list[0]=> 字符串是 'function1'=> 调用 function1()

press button again再次按下按钮

counter=1=>list[1]=>the string is 'function2' => call function2() counter=1=>list[1]=>字符串是'function2'=>调用function2()

You can call a function by its name like this:您可以按名称调用函数,如下所示:

locals()["myfunction"]()

or:或者:

globals()["myfunction"]()

or if its from another module like this:或者如果它来自像这样的另一个模块:

import foo
getattr(foo, 'myfunction')()

Or if it suits your use case, just use a list of functions instead of a list of strings:或者,如果它适合您的用例,只需使用函数列表而不是字符串列表:

def func1():
    print("1")

def func2():
    print("2")

def func3():
    print("3")

def func4():
    print("4")

some_list=[func1, func2, func3, func4]

# call like this
for f in some_list:
    f()

# or like this
some_list[0]()

Similar to what @chepner said, I would approach the problem that way.与@chepner 所说的类似,我会以这种方式解决问题。

Potentially storing the functions in a dictionary and looking up the function based on the counter:可能将函数存储在字典中并根据计数器查找函数:

def function():
    print('function 1 called')

def function2():
    print('function 2 called')

counter = 0

functions = {
    1: function,
    2: function2
}

Then:然后:

function_to_call = functions[counter + 1]

and now that when function_to_call() is called it would print function 1 called现在,当function_to_call()被调用时,它会打印function 1 called

This is how I would think about approaching the problem.这就是我如何看待解决问题的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你能调用/使用从 Python 列表返回的函数吗? - Can you call/use a function returned from a list in Python? 如何在Python中将对类/函数的调用转换为字符串? - How do you convert a call to a class/function to a string in Python? 如何从 Python 中的 function 对象列表中获取函数的全名和 arguments 作为字符串? - How do I get a function's full name and arguments as string from a list of function objects in Python? 如何使用在 Python 中的另一个 function 中创建的列表? - How do you use a list that was created in another function in Python? 如何从变量调用函数? (Python) - How do you call a Function from a Variable? (Python) Python:当你只有方法的字符串名称时,如何调用方法? - Python: How do you call a method when you only have the string name of the method? 如何通过名称(字符串)调用当前function中定义的一个function - How do you call a function defined in the current function by its name (a string) Python从具有相同名称的字符串追加到列表 - Python appending to a list from a string with the same name 您如何使用Python从CSV打印文件列表? - How do you use Python to print a list of files from a CSV? 您如何调用每个名称定义为列表的文件名? - How do you call a file name defined as list by each name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM