+ 2

What's the different uses of decorators

17th Oct 2020, 2:01 PM
Belkhir Chaimaa
Belkhir Chaimaa - avatar
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?
24th Oct 2020, 4:45 PM
Fact Finder
Fact Finder - avatar
+ 1
Thank you , I think it is correct So the main reason for the use of this function is to decorate our texts
24th Oct 2020, 5:30 PM
Belkhir Chaimaa
Belkhir Chaimaa - avatar