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

Course Content

Mastering Python: Closures and Decorators

Mastering Python: Closures and Decorators

1. Scopes
2. Closure
3. Decorators

bookChallenge: Type Validation Decorator

Task

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 4
toggle bottom row

bookChallenge: Type Validation Decorator

Task

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 4
toggle bottom row

bookChallenge: Type Validation Decorator

Task

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Task

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt