[英]Python: How to use for loop to call all the functions?
I want to use the f_i function like this:我想像这样使用 f_i function:
for i in range (9):
f_i(y)=c
But I don't know how to use the value of i to call all my ten functions.但是我不知道如何使用 i 的值来调用我所有的十个函数。 So what should I do?所以我该怎么做?
Make a list containing the functions and use each function by using loop as follows.制作一个包含函数的列表,并使用如下循环使用每个 function。
def f1(x):
return x
def f2(x):
return x * 2
def f3(x):
return x * 3
function_list = [f1, f2, f3]
for f in function_list:
print(f(1))
Assume functions is defined/imported into the current file.假设函数被定义/导入到当前文件中。
locals().get()
is the thing that you want locals().get()
是你想要的东西
for i in range (9):
func = locals().get("f_{}".format(i))
func(y)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.