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

Conteúdo do Curso

Mastering Python: Closures and Decorators

Mastering Python: Closures and Decorators

1. Scopes
2. Closure
3. Decorators

Challenge: Type Validation Decorator

Tarefa

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.

Tarefa

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.

Tudo estava claro?

Seção 3. Capítulo 4
toggle bottom row

Challenge: Type Validation Decorator

Tarefa

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.

Tarefa

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.

Tudo estava claro?

Seção 3. Capítulo 4
toggle bottom row

Challenge: Type Validation Decorator

Tarefa

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.

Tarefa

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.

Tudo estava claro?

Tarefa

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.

Seção 3. Capítulo 4
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt