Course Content
Mastering Python: Closures and Decorators
Mastering Python: Closures and Decorators
Challenge: Flexible Type Validation Decorator
Swipe to show code editor
Create a flexible decorator for validating data types:
-
Define the validate decorator, which should take data types (
str
,int
,bool
, etc.) as arguments and annotate them using thetype
keyword. -
Define the
inner
function that takes a function (func
) as an argument. -
Define the
wrapper
function that takes*args
. -
Check if each argument is an instance of one of the specified
types
. -
Functions should return:
- The
wrapper()
should return the result offunc()
with the provided arguments. - The
inner()
should returnwrapper
without calling it. - The
validate()
should returninner()
without calling it.
-
Validate the
snake_string()
function for thestr
data type using the@validate
decorator. -
Validate the
multiply()
function for theint
orfloat
data types using the@validate
decorator.
Thanks for your feedback!
Challenge: Flexible Type Validation Decorator
Swipe to show code editor
Create a flexible decorator for validating data types:
-
Define the validate decorator, which should take data types (
str
,int
,bool
, etc.) as arguments and annotate them using thetype
keyword. -
Define the
inner
function that takes a function (func
) as an argument. -
Define the
wrapper
function that takes*args
. -
Check if each argument is an instance of one of the specified
types
. -
Functions should return:
- The
wrapper()
should return the result offunc()
with the provided arguments. - The
inner()
should returnwrapper
without calling it. - The
validate()
should returninner()
without calling it.
-
Validate the
snake_string()
function for thestr
data type using the@validate
decorator. -
Validate the
multiply()
function for theint
orfloat
data types using the@validate
decorator.
Thanks for your feedback!
Challenge: Flexible Type Validation Decorator
Swipe to show code editor
Create a flexible decorator for validating data types:
-
Define the validate decorator, which should take data types (
str
,int
,bool
, etc.) as arguments and annotate them using thetype
keyword. -
Define the
inner
function that takes a function (func
) as an argument. -
Define the
wrapper
function that takes*args
. -
Check if each argument is an instance of one of the specified
types
. -
Functions should return:
- The
wrapper()
should return the result offunc()
with the provided arguments. - The
inner()
should returnwrapper
without calling it. - The
validate()
should returninner()
without calling it.
-
Validate the
snake_string()
function for thestr
data type using the@validate
decorator. -
Validate the
multiply()
function for theint
orfloat
data types using the@validate
decorator.
Thanks for your feedback!
Swipe to show code editor
Create a flexible decorator for validating data types:
-
Define the validate decorator, which should take data types (
str
,int
,bool
, etc.) as arguments and annotate them using thetype
keyword. -
Define the
inner
function that takes a function (func
) as an argument. -
Define the
wrapper
function that takes*args
. -
Check if each argument is an instance of one of the specified
types
. -
Functions should return:
- The
wrapper()
should return the result offunc()
with the provided arguments. - The
inner()
should returnwrapper
without calling it. - The
validate()
should returninner()
without calling it.
-
Validate the
snake_string()
function for thestr
data type using the@validate
decorator. -
Validate the
multiply()
function for theint
orfloat
data types using the@validate
decorator.