Decorator with Arguments
Completed
Write repeat(times), a decorator factory that makes the decorated function run times times and return a list of the results. Read a word and a number; decorate echo(word) with @repeat(n) created at runtime and print the results joined by spaces.
Input
Two lines: word, n.
Output
One line.
Example
Input
hi 3
Output
hi hi hi
Editor
Python
Hints
A decorator with arguments is a function that returns a decorator.
That's three levels of functions.
Solution explanation
@repeat(3) first calls repeat(3), then applies the decorator it returns.
Show solution
import functools
def repeat(times):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
return [func(*args, **kwargs) for _ in range(times)]
return wrapper
return decorator
word = input()
n = int(input())
@repeat(n)
def echo(text):
return text
print(*echo(word))