Most elegant way to do something N times

Python python at python.invalid
Sun Dec 22 18:02:02 EST 2019


Le 22/12/2019 à 23:50, Stefan Ram a écrit :
> Batuhan Taskaya <isidentical at gmail.com> writes:
>> dont like this for _ in range(n): do() thing. Any suggestions?
> 
>    The participants of my beginner classes like to write:
> 
> n * do()
> 
>    for that purpose, but usuallythis does not work as intended!
> 
>    What would work sometimes is
> 
> eval( n * 'do();' )
> 

or:

class repeatly_do:
     def __init__(self,n):
         self.n = n
     def __mul__(self,action):
         for _ in range(self.n):
             action()

def do():
     print('Here I am...')

repeatly_do(5) * do


More information about the Python-list mailing list