Home > AI > Language > Python >

(interview) Decorators

## Decorators

Using only the `functools` module. Create a decorator called `prepost`.

This decorator can optionally take a `prefix` parameter.

The decorator must print `"start"` before calling the decorated function, and must print `"end"` after calling the decorated function.

The `prefix` must be prepended to the printed message `"start"` and `"end"` with a space inbetween.

The decorator is used like this:

```py
@prepost(prefix='outer')
@prepost(prefix='inner')
@prepost
def multiplication(x, y):
    print('middle')
    return x * y
    
multiplication(3, 4)
```

When the above code is executed, it must print this output:

```
outer start
inner start
 start
middle
 end
inner end
outer end
```

Please implement `prepost`:

```py
def prepost ...
```

Leave a Reply