如何连续两次或更多次调用一个函数? [英] How do I call a function twice or more times consecutively?
本文介绍了如何连续两次或更多次调用一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种简短的方法可以在Python中连续两次或更多次调用一个函数?例如:
Is there a short way to call a function twice or more consecutively in Python? For example:
do()
do()
do()
可能像这样:
3*do()
推荐答案
我会:
for _ in range(3):
do()
_
是变量的约定,该变量的值无关紧要.
The _
is convention for a variable whose value you don't care about.
您可能还会看到有人写:
You might also see some people write:
[do() for _ in range(3)]
但是
稍微贵一点,因为它会创建一个包含do()
每次调用的返回值的列表(即使它是None
),然后丢弃结果列表.除非您正在使用返回值列表,否则我不建议您使用此方法.
however that is slightly more expensive because it creates a list containing the return values of each invocation of do()
(even if it's None
), and then throws away the resulting list. I wouldn't suggest using this unless you are using the list of return values.
这篇关于如何连续两次或更多次调用一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文