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

Зміст курсу

Mastering Python: Closures and Decorators

Mastering Python: Closures and Decorators

1. Scopes
2. Closure
3. Decorators

Challenge: Type Validation Decorator

Завдання

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.

Завдання

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.

Все було зрозуміло?

Секція 3. Розділ 4
toggle bottom row

Challenge: Type Validation Decorator

Завдання

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.

Завдання

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.

Все було зрозуміло?

Секція 3. Розділ 4
toggle bottom row

Challenge: Type Validation Decorator

Завдання

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.

Завдання

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.

Все було зрозуміло?

Завдання

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.

Секція 3. Розділ 4
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt