Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Flexible 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: Flexible Type Validation Decorator

Завдання

Create a flexible decorator for validating data types:

  1. Define the validate decorator, which should take data types (str, int, bool, etc.) as arguments and annotate them using the type keyword.
  2. Define the inner function that takes a function (func) as an argument.
  3. Define the wrapper function that takes *args.
  4. Check if each argument is an instance of one of the specified types.
  5. Functions should return:
  • The wrapper() should return the result of func() with the provided arguments.
  • The inner() should return wrapper without calling it.
  • The validate() should return inner() without calling it.
  1. Validate the snake_string() function for the str data type using the @validate decorator.
  2. Validate the multiply() function for the int or float data types using the @validate decorator.

Завдання

Create a flexible decorator for validating data types:

  1. Define the validate decorator, which should take data types (str, int, bool, etc.) as arguments and annotate them using the type keyword.
  2. Define the inner function that takes a function (func) as an argument.
  3. Define the wrapper function that takes *args.
  4. Check if each argument is an instance of one of the specified types.
  5. Functions should return:
  • The wrapper() should return the result of func() with the provided arguments.
  • The inner() should return wrapper without calling it.
  • The validate() should return inner() without calling it.
  1. Validate the snake_string() function for the str data type using the @validate decorator.
  2. Validate the multiply() function for the int or float data types using the @validate decorator.

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

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

Challenge: Flexible Type Validation Decorator

Завдання

Create a flexible decorator for validating data types:

  1. Define the validate decorator, which should take data types (str, int, bool, etc.) as arguments and annotate them using the type keyword.
  2. Define the inner function that takes a function (func) as an argument.
  3. Define the wrapper function that takes *args.
  4. Check if each argument is an instance of one of the specified types.
  5. Functions should return:
  • The wrapper() should return the result of func() with the provided arguments.
  • The inner() should return wrapper without calling it.
  • The validate() should return inner() without calling it.
  1. Validate the snake_string() function for the str data type using the @validate decorator.
  2. Validate the multiply() function for the int or float data types using the @validate decorator.

Завдання

Create a flexible decorator for validating data types:

  1. Define the validate decorator, which should take data types (str, int, bool, etc.) as arguments and annotate them using the type keyword.
  2. Define the inner function that takes a function (func) as an argument.
  3. Define the wrapper function that takes *args.
  4. Check if each argument is an instance of one of the specified types.
  5. Functions should return:
  • The wrapper() should return the result of func() with the provided arguments.
  • The inner() should return wrapper without calling it.
  • The validate() should return inner() without calling it.
  1. Validate the snake_string() function for the str data type using the @validate decorator.
  2. Validate the multiply() function for the int or float data types using the @validate decorator.

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

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

Challenge: Flexible Type Validation Decorator

Завдання

Create a flexible decorator for validating data types:

  1. Define the validate decorator, which should take data types (str, int, bool, etc.) as arguments and annotate them using the type keyword.
  2. Define the inner function that takes a function (func) as an argument.
  3. Define the wrapper function that takes *args.
  4. Check if each argument is an instance of one of the specified types.
  5. Functions should return:
  • The wrapper() should return the result of func() with the provided arguments.
  • The inner() should return wrapper without calling it.
  • The validate() should return inner() without calling it.
  1. Validate the snake_string() function for the str data type using the @validate decorator.
  2. Validate the multiply() function for the int or float data types using the @validate decorator.

Завдання

Create a flexible decorator for validating data types:

  1. Define the validate decorator, which should take data types (str, int, bool, etc.) as arguments and annotate them using the type keyword.
  2. Define the inner function that takes a function (func) as an argument.
  3. Define the wrapper function that takes *args.
  4. Check if each argument is an instance of one of the specified types.
  5. Functions should return:
  • The wrapper() should return the result of func() with the provided arguments.
  • The inner() should return wrapper without calling it.
  • The validate() should return inner() without calling it.
  1. Validate the snake_string() function for the str data type using the @validate decorator.
  2. Validate the multiply() function for the int or float data types using the @validate decorator.

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

Завдання

Create a flexible decorator for validating data types:

  1. Define the validate decorator, which should take data types (str, int, bool, etc.) as arguments and annotate them using the type keyword.
  2. Define the inner function that takes a function (func) as an argument.
  3. Define the wrapper function that takes *args.
  4. Check if each argument is an instance of one of the specified types.
  5. Functions should return:
  • The wrapper() should return the result of func() with the provided arguments.
  • The inner() should return wrapper without calling it.
  • The validate() should return inner() without calling it.
  1. Validate the snake_string() function for the str data type using the @validate decorator.
  2. Validate the multiply() function for the int or float data types using the @validate decorator.

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