+ 2

Decorators in python.

I made the decorators course in solo learn but I don't have pro, so I couldn't try it. I understand nothing! Can someone explain me decorators, higher order functions and this functions in functions AND give me examples why and how to use? This course I so short and badly to understand!!!

21st Aug 2025, 7:40 AM
Jakob
Jakob - avatar
8 Respostas
+ 5
you don't need a pro account to write decorator functions. there are external sources that can help you learn about decorators. https://realpython.com/primer-on-JUMP_LINK__&&__python__&&__JUMP_LINK-decorators/ yes, i agree, the courses have become less and less educational with each revision. not enough explanation and examples, all for the sake of keeping the lesson format consistent and 'cute'. a case of form over function. anyway, decorators are higher-order functions. they accept functions as arguments. they call the function internally and perform additional operations on the result. the @decorator is just a convenient syntax sugar. the lesson have this example: def uppercase(func): def wrapper(): orig_message = func() modified_message = orig_message.upper() return modified_message return wrapper @uppercase def greet(): return "Welcome!" # Using the decorated function print(greet()) if you comment out @uppercase, you could use uppercase like: print(uppercase(greet)())
21st Aug 2025, 8:27 AM
Bob_Li
Bob_Li - avatar
+ 3
Thank you! This article is so much better than the sololearn course!
21st Aug 2025, 9:30 AM
Jakob
Jakob - avatar
+ 1
why this ⬇️? uppercase(greet)() because uppercase returns another function -> wrapper. you have to call that to get the final result. uppercase(greet)() is the same as wrapper(). it's awkward, thus the @uppercase decorator syntax. then you just have to use greet().
21st Aug 2025, 9:12 AM
Bob_Li
Bob_Li - avatar
+ 1
Can you give an example for what it is really useful?
21st Aug 2025, 12:56 PM
Jakob
Jakob - avatar
+ 1
the RealPython link have a few examples at the end. i think it's commonly used in Flask, class decorators, lru cache, etc...
21st Aug 2025, 1:11 PM
Bob_Li
Bob_Li - avatar
+ 1
But RealPython isn't free
21st Aug 2025, 1:51 PM
Jakob
Jakob - avatar
+ 1
some articles are free.
21st Aug 2025, 1:54 PM
Bob_Li
Bob_Li - avatar
+ 1
Ok
21st Aug 2025, 1:58 PM
Jakob
Jakob - avatar