Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Type Validation Decorator | Decorators
Mastering Python: Closures and Decorators
course content

Contenido del Curso

Mastering Python: Closures and Decorators

Mastering Python: Closures and Decorators

1. Scopes
2. Closure
3. Decorators

Challenge: Type Validation Decorator

Tarea

Create a decorator that validates the input data for a function, and ensure that it is flexible enough to handle functions that take a varying number of arguments.

  1. Define the int_validate decorator that takes a func (function) as an argument.
  2. Inside int_validate(), define a wrapper function that takes a varying number of arguments.
  3. For each argument, check if its type is int. If the type is not int, raise a TypeError with the message "The function expects the integer type".
  4. Call the func() with the arguments taken by wrapper(), and return its result (don't forget to unpack the arguments).
  5. The int_validate() decorator should return the wrapper function without calling it.
  6. Decorate the int_sum() function using the int_validate decorator.

Tarea

Create a decorator that validates the input data for a function, and ensure that it is flexible enough to handle functions that take a varying number of arguments.

  1. Define the int_validate decorator that takes a func (function) as an argument.
  2. Inside int_validate(), define a wrapper function that takes a varying number of arguments.
  3. For each argument, check if its type is int. If the type is not int, raise a TypeError with the message "The function expects the integer type".
  4. Call the func() with the arguments taken by wrapper(), and return its result (don't forget to unpack the arguments).
  5. The int_validate() decorator should return the wrapper function without calling it.
  6. Decorate the int_sum() function using the int_validate decorator.

¿Todo estuvo claro?

Sección 3. Capítulo 4
toggle bottom row

Challenge: Type Validation Decorator

Tarea

Create a decorator that validates the input data for a function, and ensure that it is flexible enough to handle functions that take a varying number of arguments.

  1. Define the int_validate decorator that takes a func (function) as an argument.
  2. Inside int_validate(), define a wrapper function that takes a varying number of arguments.
  3. For each argument, check if its type is int. If the type is not int, raise a TypeError with the message "The function expects the integer type".
  4. Call the func() with the arguments taken by wrapper(), and return its result (don't forget to unpack the arguments).
  5. The int_validate() decorator should return the wrapper function without calling it.
  6. Decorate the int_sum() function using the int_validate decorator.

Tarea

Create a decorator that validates the input data for a function, and ensure that it is flexible enough to handle functions that take a varying number of arguments.

  1. Define the int_validate decorator that takes a func (function) as an argument.
  2. Inside int_validate(), define a wrapper function that takes a varying number of arguments.
  3. For each argument, check if its type is int. If the type is not int, raise a TypeError with the message "The function expects the integer type".
  4. Call the func() with the arguments taken by wrapper(), and return its result (don't forget to unpack the arguments).
  5. The int_validate() decorator should return the wrapper function without calling it.
  6. Decorate the int_sum() function using the int_validate decorator.

¿Todo estuvo claro?

Sección 3. Capítulo 4
toggle bottom row

Challenge: Type Validation Decorator

Tarea

Create a decorator that validates the input data for a function, and ensure that it is flexible enough to handle functions that take a varying number of arguments.

  1. Define the int_validate decorator that takes a func (function) as an argument.
  2. Inside int_validate(), define a wrapper function that takes a varying number of arguments.
  3. For each argument, check if its type is int. If the type is not int, raise a TypeError with the message "The function expects the integer type".
  4. Call the func() with the arguments taken by wrapper(), and return its result (don't forget to unpack the arguments).
  5. The int_validate() decorator should return the wrapper function without calling it.
  6. Decorate the int_sum() function using the int_validate decorator.

Tarea

Create a decorator that validates the input data for a function, and ensure that it is flexible enough to handle functions that take a varying number of arguments.

  1. Define the int_validate decorator that takes a func (function) as an argument.
  2. Inside int_validate(), define a wrapper function that takes a varying number of arguments.
  3. For each argument, check if its type is int. If the type is not int, raise a TypeError with the message "The function expects the integer type".
  4. Call the func() with the arguments taken by wrapper(), and return its result (don't forget to unpack the arguments).
  5. The int_validate() decorator should return the wrapper function without calling it.
  6. Decorate the int_sum() function using the int_validate decorator.

¿Todo estuvo claro?

Tarea

Create a decorator that validates the input data for a function, and ensure that it is flexible enough to handle functions that take a varying number of arguments.

  1. Define the int_validate decorator that takes a func (function) as an argument.
  2. Inside int_validate(), define a wrapper function that takes a varying number of arguments.
  3. For each argument, check if its type is int. If the type is not int, raise a TypeError with the message "The function expects the integer type".
  4. Call the func() with the arguments taken by wrapper(), and return its result (don't forget to unpack the arguments).
  5. The int_validate() decorator should return the wrapper function without calling it.
  6. Decorate the int_sum() function using the int_validate decorator.

Sección 3. Capítulo 4
Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt