Answers for "why do we need to use decorators in python"

23

python decorators

# decorators are user to decorate functions like what to do before/after calling the function
import time

def delay_decorator(function):
    def wrapper_function():
        print("-----------------------I am gonna greet you--------------------------")
        time.sleep(2)# waits for 2 seconds
        function()   # calls the function
        time.sleep(1)# waits for 1 sec
        print("------------------How do you feel about that greet?-------------------")
    return wrapper_function

@delay_decorator
def greet():
    print("Helllllloooooooooo")

greet()
Posted by: Guest on March-23-2021
0

decorators in python

def deco(func):
    def wrap(s):
        d = {}
        for i in s:
            d[i] = s.count(i)
        func(s)
        return d
    return wrap

@deco
def count(stng):
    return stng
mystring = "scorpio"  
print(count(mystring))
Posted by: Guest on June-28-2021

Code answers related to "why do we need to use decorators in python"

Python Answers by Framework

Browse Popular Code Answers by Language