Idiomatic Python: 'times' loop [duplicate]
If you want the times
method, and you need to use it on your own functions, try this:
def times(self, n, *args, **kwargs): for _ in range(n): self.__call__(*args, **kwargs)import newdef repeatable(func): func.times = new.instancemethod(times, func, func.__class__) return func
now add a @repeatable
decorator to any method you need a times
method on:
@repeatabledef foo(bar): print barfoo.times(4, "baz") #outputs 4 lines of "baz"