# decorators are user to decorate functions like what to do before/after calling the functionimport time
defdelay_decorator(function):
defwrapper_function():
print("-----------------------I am gonna greet you--------------------------")
time.sleep(2)# waits for 2 secondsfunction() # calls the function time.sleep(1)# waits for 1 secprint("------------------How do you feel about that greet?-------------------")
return wrapper_function
@delay_decorator
defgreet():
print("Helllllloooooooooo")
greet()
Posted by: Guest
on March-23-2021
0
python decorator
import functools
# A decorator is a Higher order function"_with_logging"# It takes the function to be decorated as its argument# In order to pass in some arbitrary arguments, it must be wrapped into# another HOF (Higher order function) that will receive the inputsdefwith_logging(level=logging.DEBUG, msg = None):
def_with_logging(fn):
# 'wraps' is a HOF that will give fn's name and docs to decorated_fn i.e. # decorated_fn.__name__ = fn.__name__# help(decorated_fn) = help(fn)
@functools.wraps(fn)
defdecorated_fn(*args, **kwargs):
res =fn(*args, **kwargs)
print("n***************", f"n{msg}", "nExecuting with Args: ", *args, **kwargs)
logging.log(level, res)
return res
return decorated_fn
return _with_logging
# Used this way
@with_logging(level=logging.DEBUG, msg="Some awesome comment")
defhello_world(name):
return f'Hello World {name}'
# Results after calling hello_world("John")
## ***************# Some awesome comment# Executing with Args: John# Hello World John
Posted by: Guest
on March-14-2021
Code answers related to "python create a decorator"
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems
resetting your password contact us
Check Your Email and Click on the link sent to your email