+ 2
What's the different uses of decorators
2 ответов
+ 1
For example you have to add a decoration to 2 of your functions:
def hi():
print("Hi")
def hello():
print("hello")
But you want these functions to have "==================" before and after what they are output-ing.
So instead of doing this 👇:
def hi():
print("==================")
print("Hi")
print("==================")
def hello():
print("==================")
print("hello")
print("==================")
You can simply use the decorator by defining another function as the decorator
def decor(func):
print("==================")
func()
print("==================")
@decor
def hi():
print("Hi")
@decor
def hello():
print("hello")
It just saves you time if you are working with many functions
Output :
==================
Hi
==================
==================
Hello
==================
Am I right? I think the code is correct. Is it?
+ 1
Thank you , I think it is correct
So the main reason for the use of this function is to decorate our texts