Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Create Your Decorator | Decorators
Intermediate Python: Arguments, Scopes and Decorators

Challenge: Create Your DecoratorChallenge: Create Your Decorator

Tarea

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:

  1. You need to define the parameters that the wrapper will accept;
  2. Call the func function with the arguments *args and **kwargs, and assign it to the result variable;
  3. In the line return ___, you should return result;
  4. Complete the decorator by returning the wrapper function;
  5. Applies the time_it decorator to the factorial function;
  6. Calls the factorial function (which is now decorated with time_it) and prints its return value.

¿Todo estuvo claro?

Sección 5. Capítulo 3
toggle bottom row
course content

Contenido del Curso

Intermediate Python: Arguments, Scopes and Decorators

Challenge: Create Your DecoratorChallenge: Create Your Decorator

Tarea

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:

  1. You need to define the parameters that the wrapper will accept;
  2. Call the func function with the arguments *args and **kwargs, and assign it to the result variable;
  3. In the line return ___, you should return result;
  4. Complete the decorator by returning the wrapper function;
  5. Applies the time_it decorator to the factorial function;
  6. Calls the factorial function (which is now decorated with time_it) and prints its return value.

¿Todo estuvo claro?

Sección 5. Capítulo 3
toggle bottom row
some-alt