如何使用for循环遍历函数? [英] How do I loop through functions with a for loop?

查看:151
本文介绍了如何使用for循环遍历函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的功能不同,但名称中的数字越来越大,该如何遍历它们?例如:

If I have different functions with increasing numbers in their names how do I loop through them? For example:

def Func1():
    something something

def Func2():
    something something

def Func3():
    something something

我可以用一个循环遍历它们吗?

can I loop through them with a:

for i in range(1,4):

推荐答案

您可以将函数放在列表中并在其上循环:

You can put the functions in a list and loop over that:

for func in [Func1, Func2, Func3]:
    result = func()

函数是Python中的一流对象,您可以创建对它们的(附加)引用,并从任何引用中调用它们.

Functions are first-class objects in Python, you can create (additional) references to them and call them from whatever reference you have.

这篇关于如何使用for循环遍历函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆