+ 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!!!
21 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)())
+ 3
Thank you! This article is so much better than the sololearn course!
+ 2
Moi non
+ 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().
or maybe you can assign the returned function to a variable:
decorated_function = uppercase(greet)
print(decorated_function())
it's awkward, thus the @uppercase decorator syntax. then you just have to use greet().
+ 1
Can you give an example for what it is really useful?
+ 1
the RealPython link have a few examples at the end. i think it's commonly used in Flask, class decorators, lru cache, etc...
+ 1
But RealPython isn't free
+ 1
some articles are free.
+ 1
Ok
+ 1
This is not the right place!
But why do you don't start a course if you are new?
+ 1
Qui est français ici
+ 1
Vous comprenez français non
+ 1
Je comprends un peu
+ 1
Là c'est bien tu est unpeu doué en développement Web non ont peut échanger les contacts whatsapp pour s'entraîner
+ 1
Tu pourrais aussi simplement m'écrire via sololearn. Mais je ne suis pas vraiment doué pour le développement web.
+ 1
Abon vraiment
+ 1
Je ne comprends pas.
+ 1
Non ça va OK mais tu n'es doué à quelque part
0
Abeg I need help
0
I am just new, I don't know anything