Advanced functions and decorators · 12 / 15 Exercise Medium

Decorator that logs calls

Objective

Create a Python program with your first decorator: a function that wraps another one to add behaviour to it.

Instructions

  • Define def log_calls(function). Inside, define wrapper(*args, **kwargs), which shows Calling NAME with ARGS (use function.__name__ and the args tuple), calls the original function and returns its result. log_calls returns wrapper.
  • Define add(a, b), which returns a + b, and decorate it with @log_calls.
  • Read two integers (one per line), call add and show Result: R.
Input
3
4
Output
Calling add with (3, 4)
Result: 7

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

Start exercise