Command Palette

Search for a command to run...

Hectal
Phase 9: Iterators, Generators and Decorators
Python · Exercise 6 of 6 Previous
Advanced

Write a context manager that always cleans up

Using @contextmanager, write logged(name). On entering the with block it prints enter <name>; on leaving it prints exit <name>, even if the block raises an error. The error must still reach the caller. It should yield the name in upper case.

Use it around a print("working") and print the yielded name.

What Check tests

  1. Normal use prints enter, the body, then exit
  2. It yields the name in upper case
  3. exit is printed even when the body raises
  4. The error still reaches the caller
  5. The program prints the four lines

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.