Lab, Decorators and Context Managers
7 tasks, easy to hard: Predict, then run, Write it, Complete it, Fix it, Trace it, Refactor it, Mini challenge. Hints come one at a time and you can see each answer. Lab work is saved as practice and earns no XP.
Open the lessonYou can read every task below. Running, checking and saving them needs a free ID.
7 tasks
Task 01 .Type what the program will print, then run it to compare.
Type exactly what this program prints. Notice which lines run when the function is defined and which run when it is called.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 02 .Solve it from the prompt, starting from an empty editor.
Write a decorator announce that prints calling NAME (the wrapped function's name) before each call, works for any arguments, returns the original result and keeps the name with functools.wraps. Apply it to area(w, h) and print area(3, 4) and area.__name__.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 03 .Fill the marked gaps in the starter code.
Replace ___ so the decorator hands back its wrapper and the program prints <b>News</b>.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 04 .The starter code has a bug. Repair it so the checks pass.
The program should print log: total and then 60, but it prints None instead of the total. Fix the decorator.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 05 .Fill the table with the variable values, step by step.
Each row shows n and total each time one of them changes, once both exist. double is decorated, but it still returns twice its input.
def count_calls(func):
def wrapper(*args):
wrapper.calls += 1
return func(*args)
wrapper.calls = 0
return wrapper
@count_calls
def double(n):
return n * 2
total = 0
for n in [1, 2, 3]:
total = total + double(n)Sign in or create a free ID to fill in and check this table.
Task 06 .Rewrite working code to the stated rule, keeping the same output. Rule: One logged decorator prints the name from func.__name__; no function prints its own running line.
This code works, but every function repeats its own print("running ...") line. Write a decorator logged that prints running NAME using func.__name__, apply it with @logged, and remove the print lines from the three functions. The output must not change.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 07 .A short task that combines this lesson with earlier ones.
Write a decorator factory limit_calls(n). A decorated function may be called n times; after that each call raises RuntimeError with the message NAME called more than N times. Each decorated function has its own count, and keeps its name with functools.wraps. The program should print:
code 1 code 2 RuntimeError: send_code called more than 2 times hi send_code
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.