How do I call a function twice or more times consecutively?

后端 未结 9 1825
借酒劲吻你
借酒劲吻你 2020-12-05 00:02

Is there a short way to call a function twice or more consecutively in Python? For example:

do()
do()
do()

maybe like:

3*do         


        
9条回答
  •  攒了一身酷
    2020-12-05 00:34

    from itertools import repeat, starmap
    
    results = list(starmap(do, repeat((), 3)))
    

    See the repeatfunc recipe from the itertools module that is actually much more powerful. If you need to just call the method but don't care about the return values you can use it in a for loop:

    for _ in starmap(do, repeat((), 3)): pass
    

    but that's getting ugly.

提交回复
热议问题