Course Content
Intermediate Python: Arguments, Scopes and Decorators
Intermediate Python: Arguments, Scopes and Decorators
Challenge: Create Your Decorator
Task
Sometimes, it's necessary to measure how long a particular function or code segment takes to execute. This can be achieved by calculating the time elapsed from the start to the end of the function. However, if you need to measure the execution time of multiple functions, a more efficient approach would be to write a single decorator and apply it to these functions.
We'll develop such a decorator and see its effectiveness:
- You need to define the parameters that the wrapper will accept;
- Call the
func
function with the arguments *args and **kwargs, and assign it to theresult
variable; - In the line return ___, you should return
result
; - Complete the decorator by returning the wrapper function;
- Applies the
time_it
decorator to thefactorial
function; - Calls the
factorial
function (which is now decorated with time_it) and prints its return value.
Thanks for your feedback!
Challenge: Create Your Decorator
Task
Sometimes, it's necessary to measure how long a particular function or code segment takes to execute. This can be achieved by calculating the time elapsed from the start to the end of the function. However, if you need to measure the execution time of multiple functions, a more efficient approach would be to write a single decorator and apply it to these functions.
We'll develop such a decorator and see its effectiveness:
- You need to define the parameters that the wrapper will accept;
- Call the
func
function with the arguments *args and **kwargs, and assign it to theresult
variable; - In the line return ___, you should return
result
; - Complete the decorator by returning the wrapper function;
- Applies the
time_it
decorator to thefactorial
function; - Calls the
factorial
function (which is now decorated with time_it) and prints its return value.
Thanks for your feedback!
Challenge: Create Your Decorator
Task
Sometimes, it's necessary to measure how long a particular function or code segment takes to execute. This can be achieved by calculating the time elapsed from the start to the end of the function. However, if you need to measure the execution time of multiple functions, a more efficient approach would be to write a single decorator and apply it to these functions.
We'll develop such a decorator and see its effectiveness:
- You need to define the parameters that the wrapper will accept;
- Call the
func
function with the arguments *args and **kwargs, and assign it to theresult
variable; - In the line return ___, you should return
result
; - Complete the decorator by returning the wrapper function;
- Applies the
time_it
decorator to thefactorial
function; - Calls the
factorial
function (which is now decorated with time_it) and prints its return value.
Thanks for your feedback!
Task
Sometimes, it's necessary to measure how long a particular function or code segment takes to execute. This can be achieved by calculating the time elapsed from the start to the end of the function. However, if you need to measure the execution time of multiple functions, a more efficient approach would be to write a single decorator and apply it to these functions.
We'll develop such a decorator and see its effectiveness:
- You need to define the parameters that the wrapper will accept;
- Call the
func
function with the arguments *args and **kwargs, and assign it to theresult
variable; - In the line return ___, you should return
result
; - Complete the decorator by returning the wrapper function;
- Applies the
time_it
decorator to thefactorial
function; - Calls the
factorial
function (which is now decorated with time_it) and prints its return value.