Advanced functions and decorators · 14 / 15 Exercise Medium

Decorator with a parameter

Objective

Create a Python program with a decorator that receives a parameter: it needs three levels of functions.

Instructions

  • Define def repeat(times), which returns a decorator. That decorator receives a function and returns a wrapper(*args, **kwargs) that calls the original function times times in a row.
  • Define say_hello(name), which shows Hello, NAME, and decorate it with @repeat(times). Since times is read from the input, apply the decorator by hand: say_hello = repeat(times)(say_hello).
  • Read the number of times on the first line and the name on the second.
Input
3
Ana
Output
Hello, Ana
Hello, Ana
Hello, Ana

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise