Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: Logging and Timing Decorators | Mastering Python Decorators
Functional Programming Concepts in Python
Osio 4. Luku 8
single

single

Challenge: Logging and Timing Decorators

Pyyhkäise näyttääksesi valikon

Tehtävä

Pyyhkäise aloittaaksesi koodauksen

  • Add @functools.wraps(func) above each wrapper.
  • In log_calls, complete log_message to print the function name, positional arguments, and keyword arguments.
  • In time_execution, before calling the function, store the start time with time.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_calls outer, time_execution inner.

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 8
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

some-alt