Seksjon 4. Kapittel 8
single
Challenge: Logging and Timing Decorators
Sveip for å vise menyen
Oppgave
Sveip for å begynne å kode
- Add
@functools.wraps(func)above each wrapper. - In
log_calls, completelog_messageto print the function name, positional arguments, and keyword arguments. - In
time_execution, before calling the function, store the start time withtime.time(). - After calling the function, store the end time.
- Calculate and print the execution time.
- Print both the log and timing messages.
- Decorator order:
log_callsouter,time_executioninner.
Expected result:
When you call multiply(5, 7), the output should look like:
Calling function 'multiply' with args=(5, 7), kwargs={}
Function 'multiply' executed in X.XXXXXX seconds
35
Where X.XXXXXX is the execution time formatted to six decimal places. The function should return 35.
Løsning
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 4. Kapittel 8
single
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår