Course Content
Mastering Python: Closures and Decorators
Mastering Python: Closures and Decorators
Challenge: 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.
- Define the
int_validate
decorator that takes afunc
(function) as an argument. - Inside
int_validate()
, define awrapper
function that takes a varying number of arguments. - For each argument, check if its type is
int
. If the type is not int, raise aTypeError
with the message"The function expects the integer type"
. - Call the
func()
with the arguments taken bywrapper()
, and return its result (don't forget to unpack the arguments). - The
int_validate()
decorator should return thewrapper
function without calling it. - Decorate the
int_sum()
function using theint_validate
decorator.
Thanks for your feedback!
Challenge: 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.
- Define the
int_validate
decorator that takes afunc
(function) as an argument. - Inside
int_validate()
, define awrapper
function that takes a varying number of arguments. - For each argument, check if its type is
int
. If the type is not int, raise aTypeError
with the message"The function expects the integer type"
. - Call the
func()
with the arguments taken bywrapper()
, and return its result (don't forget to unpack the arguments). - The
int_validate()
decorator should return thewrapper
function without calling it. - Decorate the
int_sum()
function using theint_validate
decorator.
Thanks for your feedback!
Challenge: 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.
- Define the
int_validate
decorator that takes afunc
(function) as an argument. - Inside
int_validate()
, define awrapper
function that takes a varying number of arguments. - For each argument, check if its type is
int
. If the type is not int, raise aTypeError
with the message"The function expects the integer type"
. - Call the
func()
with the arguments taken bywrapper()
, and return its result (don't forget to unpack the arguments). - The
int_validate()
decorator should return thewrapper
function without calling it. - Decorate the
int_sum()
function using theint_validate
decorator.
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.
- Define the
int_validate
decorator that takes afunc
(function) as an argument. - Inside
int_validate()
, define awrapper
function that takes a varying number of arguments. - For each argument, check if its type is
int
. If the type is not int, raise aTypeError
with the message"The function expects the integer type"
. - Call the
func()
with the arguments taken bywrapper()
, and return its result (don't forget to unpack the arguments). - The
int_validate()
decorator should return thewrapper
function without calling it. - Decorate the
int_sum()
function using theint_validate
decorator.