Objective
Create a Python program with a decorator that keeps track of how many times a function has been called.
Instructions
- Define
def count_calls(function). Itswrapper(*args, **kwargs)adds 1 to a counter (an attribute of the wrapper itself:wrapper.calls, which starts at 0) and calls the original function, returning its result.count_callsreturnswrapper. - Define
greet(name), which returns"Hello, NAME", and decorate it with@count_calls. - Read an integer
nand thennnames, one per line. Show the greeting for each one. - At the end show
Calls: K, readinggreet.calls.
2
Ana
Luis
Hello, Ana
Hello, Luis
Calls: 2
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise